I tried a bit of code but it seems to cause issues:
class Page:
cache = []
""" Return cached object """
def __getCache(self, title):
for o in Page.cache:
if o.__searchTerm == title or o.title == title:
return o
return None
""" Initilize the class and start processing """
def __init__(self, title, api=None):
o = self.__getCache(title)
if o:
self = o
return
Page.cache.append(self)
# Other init code
self.__searchTerm = title
self.title = self.someFunction(title)
Then I try:
a = Page('test')
b = Page('test')
print a.title # works
print b.title # AttributeError: Page instance has no attribute 'title'
Whats wrong with this bit of code? Why wont it work? Is there a way to make it work? If not how do I go about easily and transparently to the end user caching objects?
if you want to manipulate the creation, you need to change __new__
.
>>> class Page(object):
... cache = []
... """ Return cached object """
... @classmethod
... def __getCache(cls, title):
... for o in Page.cache:
... if o.__searchTerm == title or o.title == title:
... return o
... return None
... """ Initilize the class and start processing """
... def __new__(cls, title, api=None):
... o = cls.__getCache(title)
... if o:
... return o
... page = super(Page, cls).__new__(cls)
... cls.cache.append(page)
... page.title = title
... page.api = api
... page.__searchTerm = title
... # ...etc
... return page
...
>>> a = Page('test')
>>> b = Page('test')
>>>
>>> print a.title # works
test
>>> print b.title
test
>>>
>>> assert a is b
>>>
EDIT: using __init__
:
>>> class Page(object):
... cache = []
... @classmethod
... def __getCache(cls, title):
... """ Return cached object """
... for o in Page.cache:
... if o.__searchTerm == title or o.title == title:
... return o
... return None
... def __new__(cls, title, *args, **kwargs):
... """ Initilize the class and start processing """
... existing = cls.__getCache(title)
... if existing:
... return existing
... page = super(Page, cls).__new__(cls)
... return page
... def __init__(self, title, api=None):
... if self in self.cache:
... return
... self.cache.append(self)
... self.title = title
... self.api = api
... self.__searchTerm = title
... # ...etc
...
>>>
>>> a = Page('test')
>>> b = Page('test')
>>>
>>> print a.title # works
test
>>> print b.title
test
>>> assert a is b
>>> assert a.cache is Page.cache
>>>
You cannot really change the instance of a created object once it has been created. When setting self
to something else, all you do is change the reference that variable points to, so the actual object is not affected.
This also explains why the title
attribute is not there. You return as soon as you change the local self
variable, preventing the current instance to get the title
attribute initialized (not to mention that self
at that point wouldn’t point to the right instance).
So basically, you cannot change the object during its initialization (in __init__
) as at that point it has already been created, and assigned to the variable. A constructor call like a = Page('test')
is actually the same as:
a = Page.__new__('test')
a.__init__('test')
So as you can see, the __new__
class constructor is called first, and that’s actually who’s responsible for creating the instance. So you could overwrite the class’ __new__
method to manipulate the object creation.
A generally preferred way however is to create a simple factory method, like this:
@classmethod
def create (cls, title, api = None):
o = cls.__getCache(title)
if o:
return o
return cls(title, api)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With