You cannot change an instance of an created object after it is created. When setting self to something else, all you do is change the link the variable points to, so the actual object is not affected.
This also explains why the title attribute is missing. You return as soon as you change the local variable self , not allowing the current instance to initialize the title attribute (not to mention the fact that self at this point does not point to the correct instance).
Thus, you cannot change the object during its initialization (in __init__ ), since at that moment it was already created and assigned to a variable. A constructor call of type a = Page('test') is actually the same as:
a = Page.__new__('test') a.__init__('test')
So, as you can see, the __new__ class constructor call is first made, and this is actually responsible for instantiating. This way you can overwrite the __new__ class method to manipulate the creation of the object.
Usually the preferred way is to create a simple factory method, for example:
@classmethod def create (cls, title, api = None): o = cls.__getCache(title) if o: return o return cls(title, api)
source share