I came across some python examples where a class instance is created by Invoking the class and passing "self" to it. I cant seem to understand the meaning of it and when we would use such constructs.
Below is a sample extract, where a class is instantiated inside another class. I "think" I have also seen something like objA = Class_def(self). I cant recall where I saw that, so it would be good to know if its possible or not.
class BaseState(object):
def __init__(self, protocol):
self.protocol = protocol
def connect(self, request):
state = self.__class__.__name__
class IdleState(BaseState):
def connect(self, request):
return self.protocol.doConnect(request)
class ConnectingState(BaseState):
def handleCONNACK(self, response):
self.protocol.handleCONNACK(response)
class ConnectedState(BaseState):
def disconnect(self, request):
self.protocol.doDisconnect(request)
class BaseProtocol(Protocol):
def __init__(self, factory):
###
# what is happening here -
###
self.IDLE = IdleState(self)
self.CONNECTING = ConnectingState(self)
self.CONNECTED = ConnectedState(self)
self.state = self.IDLE