A simpler singleton pattern?
lee lion
dance_code at hotmail.com
Sat Sep 28 20:31:53 EDT 2002
singleton pattern is a way to provide one and only one object of a
particular type. There are many ways to implement a singleton pattern, is
the below one?
class OnlyOne:
instance = None
def __init__(self, arg):
OnlyOne.instance = arg
def __getattr__(self, name):
return getattr(OnlyOne.instance, name)
if __name__ == "__main__":
x = OnlyOne("x")
print x
y = OnlyOne("y")
print y
print x
print `x`
print `y`
The output is:
x
y
y
'y'
'y'
"The best way of living is to earn a little more than you cost and to cost
just as you need to."
Li Hang
_________________________________________________________________
与联机的朋友进行交流,请使用 MSN Messenger:
http://messenger.microsoft.com/cn/
More information about the Python-list
mailing list