replacing one instance with another

manstey manstey at csu.edu.au
Sun Mar 25 22:34:51 EDT 2007


Hi, yet again to myself!

I've realised after further testing and reading that I actually need
to do this:

>>>dic_myinstances={}
>>>class MyClass(object):
    def __new__(cls,id):
        global dic_myinstances
        if dic_myinstances.has_key(id):
            return dic_myinstances[id]
        else:
            dic_myinstances[id] = super(MyClass, cls).__new__(cls, id)
            return dic_myinstances[id]
    def __init__(self,id):
        print id

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




More information about the Python-list mailing list