All pastes #56369 Raw Edit

Anonymous

public python v1 · immutable
#56369 ·published 2006-05-16 21:42 UTC
rendered paste body
class Singleton(object):    def __new__(type):        if not '_the_instance' in type.__dict__:            type._the_instance = object.__new__(type)        return type._the_instancea=Singleton()a.toto = 12b=Singleton()print b.totoprint id(a),id(b) # the same !!print a print b## now lets inherit from it:class Main(Singleton):    def __init__(self):    	Singleton.__init__(self)	## only init if not done already       	#if not hasattr(self, "x"):	#	self.x =4test = Main()print test#print test.x test.x = 5print test.xtest2 = Main()print test2print test2.x