Yes, a class can contain an instance of itself; you simply cannot create it when you initiate it for the reasons described by others.
For example, this class will do this,
class A:
def __init__(self,value):
self.value=value
def setProperty(self,subvalue):
self.innerInstance=A(subvalue)
Then you can create an instance and set its internal copy as follows:
>>>OuterInstance=A(123)
>>>OuterInstance.setProperty(456)
And make sure it worked with:
>>>OuterInstance.innerInstance.value
456
source
share