0

I am trying to use PodSixNet library in Python to implement a multiplayer game. As I have seen a few tutorials online, the server file is as follows:

import PodSixNet.Channel
import PodSixNet.Server
from time import sleep

class ClientChannel(PodSixNet.Channel.Channel):
    def Network(self, data):
        print data

class BoxesServer(PodSixNet.Server.Server):
    channelClass = ClientChannel

    def __init__(self, *args, **kwargs):
         PodSixNet.Server.Server.__init__(self, *args, **kwargs)

What does the line channelClass = ClientChannelmean?

channelClass is definitely not an instance of the ClientChannel class because the instance declaration is not correct. So what is it then?

5
  • It's not an instance, it's a reference to the class itself. Commented Jul 30, 2015 at 10:56
  • And Python does not have declarations. You just assign variables without declaring them. Commented Jul 30, 2015 at 10:56
  • It assigns the class ClientChannel to the "class property" channelClass. No more, no less. Likely some code within PodSixNet.Server.Server.__init__ is going to pick up on that property and do something with the class. Commented Jul 30, 2015 at 10:56
  • 1
    You are assigning ClientChannel as channelClass. Commented Jul 30, 2015 at 10:57
  • You are assigning ClientChannel, a class (the class object itself, not one of its instances) to BoxesServer.channelClass, a class attribute (shared by all instances of that class). This attribute is then used by instances of the class; see github.com/chr15m/PodSixNet/blob/master/PodSixNet/Server.py#L32 Commented Jul 30, 2015 at 10:58

3 Answers 3

4

All this does is create the BoxesServer.channelClass class attribute. It is just a reference to another class.

Why would you do that? Well, the PodSixNet.Server.Server is flexible, it doesn't hardcode the class it'll use to create channels for new connections. Instead, it'll look for the self.channelClass attribute, and use that to create new channel instances. See the Server.handle_accept() method source:

self.channels.append(self.channelClass(conn, addr, self, self._map))

Calling self.channelClass() then creates an instance of whatever class is assigned to that attribute. This lets you swap out the channel class easily when defining new subclasses.

Note that the PodSixNet.Server.Server() class can also take the channel class as an argument when creating an instance. That'll then override the class attribute you set.

Sign up to request clarification or add additional context in comments.

Comments

1

This is meant for deferred use.

Later on, any instantiation of channelClass will result in ClientChannel instances.

See this as a way to allow the developer to use whatever class he wants, but the PodSixNet.Server.Server subclasses will always instantiate channelClass as they cannot know what real class will be given by the developer.

Comments

0

In python classes are first class objects. You can assign them to a variable just like any other object. The channelClass = ClientChannel means to assign to BoxesServer.channelClass the class ClientChannel (as a class variable), later BoxesServer.channelClass can be used just like ClientChannel.

This could be useful if you derive from BoxesServer and then assign in the derived class another class for channelClass which would mean that you can customize which class to be used in place of ClientChannel. You can for example in a method write:

def getChannelInstance(self, *args, **kwds):
     return self.channelClass(*args, **kwds)

...and it will return an instance as appropriate depending on the class.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.