replacing one instance with another

manstey manstey at csu.edu.au
Sun Mar 25 21:44:17 EDT 2007


Hi,

I solved it myself! I realised that __new__ creates self prior to
init, so this works:

>>>dic_myinstances={}

>>>class MyClass(object):
    def __new__(self,id):
        global dic_myinstances
        if dic_myinstances.has_key(id):
            return dic_myinstances[id]
        else:
            dic_myinstances[id] = self
            return self


>>>ins1 = MyClass('xx')
>>>ins2 = MyClass('yy')
>>>ins3 = MyClass('xx')
>>>print ins3 is ins1
True

Is this the best way to do this?




More information about the Python-list mailing list