singleton problems

harryos oswald.harry at gmail.com
Sun Oct 3 04:55:00 EDT 2010


hi
I have been trying out singleton design pattern implementations..I
wrote this,


class Singleton(object):
    _instance = None
    def __new__(self, *args, **kwargs):
        if not self._instance:
            self._instance = super(Singleton, self).__new__(self,
*args, **kwargs)
        return self._instance


class Mysingle(Singleton):
    def __init__(self,name):
        self.name=name

if __name__=='__main__':
    s1=Mysingle('john')
    s2=Mysingle('jim')
    s3=Mysingle('jeff')
    print 's1=',s1,s1.name
    print 's2=',s2,s2.name
    print 's3=',s3,s3.name

This is the result I got
s1= <__main__.Mysingle object at 0xb776492c> jeff
s2= <__main__.Mysingle object at 0xb776492c> jeff
s3= <__main__.Mysingle object at 0xb776492c> jeff
/home/dev/eclipse_workspace/pylearn/src/designpatterns.py:11:
DeprecationWarning: object.__new__() takes no parameters
  self._instance = super(Singleton, self).__new__(self, *args,
**kwargs)

shouldn't the name of s1,s2,s3 be 'john' instead of 'jeff'?
Also,how do I correct the deprecation problem?Can somebody comment?



More information about the Python-list mailing list