8

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?

3 Answers 3

10

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
>>> 
Sign up to request clarification or add additional context in comments.

1 Comment

@BrendanLong, you can use __init__ if you like if you have a way to find out when to skip it.
4

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)

Comments

1

self is a normal local variable, so setting self = .. just changes what the self variable points to in that function. It doesn't change the actual object.

See: Is it safe to replace a self object by another object of the same type in a method?

To do what you want, you could use a static function as a factory:

class Page:
    cache = []


    """ Return cached object """
    @staticmethod
    def create(title):
        for o in Page.cache:
            if o.__searchTerm == title or o.title == title:
                return o
        return Page(title)

    """ Initilize the class and start processing """
    def __init__(self, title, api=None):
        Page.cache.append(self)

        # Other init code
        self.__searchTerm = title
        self.title = title


a = Page.create('test')
b = Page.create('test')

print a.title
print b.title

6 Comments

Is there a way to make it work? If not how do I go about easily and transparently to the end user caching objects?
Rather than setting self, tried return o but then I get yelled at by python that init should return None
@Justin808 Apparently not. I was looking at using __new__, but apparently that won't work (see poke's answer).
Thanks, I guess I'll have to go with a factory. I was hoping I could be transparent to the user, but doesn't look like python will allow that.
Is there any way to make __init__ private so o = Page('test') called by the user would fail?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.