joe-greenawalt / skulpt

Automatically exported from code.google.com/p/skulpt
Other
0 stars 0 forks source link

construction of singleton fails #89

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Report from Denis Rangel, see 
http://groups.google.com/group/skulpt/browse_thread/thread/556bc1ae9f13c741

class Singleton(object):
    _instance = None

    def __new__(cls, *args, **kwargs):
        if not cls._instance:
            cls._instance = object.__new__(cls, *args, **kwargs)

        return cls._instance

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

if __name__ == '__main__':
    p1 = People('João', 20)
    p2 = People('Maria', 85)

    print p1.name # vai escrever Maria
    print p2 is p1 # True.
    ​

Original issue reported on code.google.com by sgraham on 30 Mar 2011 at 5:53