Assigning to self

Reinhold Birkenfeld reinhold-birkenfeld-nospam at wolke7.net
Mon Jan 17 20:03:16 CET 2005


Frans Englich wrote:
> Hello,
> 
> I am having trouble with throwing class instances around. Perhaps I'm 
> approaching my goals with the wrong solution, but here's nevertheless a 
> stripped down example which demonstrates my scenario:
> 
> #------------------------------------------------------------------------------------------
> 
> class foo:
>         tests = {}
>         def __init__( self, id ):
> 
>                 try:
>                         me = self.__class__.tests[ id ]
> 
>                 except KeyError:
>                         print "Did not exist, initializing myself.."
>                         self.attr = "exists"
>                         self.__class__.tests[ id ] = self
> 
>                 else:
>                         print "Already exists! Re-using existing instance"
>                         self = me
> 
>                 print "Me", self.attr   +  "!" # line 18
> 
>         def yo(self):
>                 return self.attr # line 21

As 'self' is a method parameter, changing it only affects the current
function. When __init__ is called, the instance is already created, so
you can't change it.

What you are looking for is a class factory (is this term correct?),
here is a sample implementation (using 2.4 decorators):

class foo:
    tests = {}
    @classmethod
    def get_test(cls, id):
        if cls.tests.has_key(id):
            return cls.tests[id]
        else:
            inst = cls()
            inst.attr = "exists"
            cls.tests[id] = inst
            return inst

    def yo(self):
        return self.attr

Here you define get_test as a classmethod, that is, it does not receive
the instance as first argument, but the class. It can be called from the
class (foo.get_test) or an instance (foo().get_test).

An alternative might be to override __new__, but I'm sure someone other
will suggest this.

Reinhold



More information about the Python-list mailing list