We don't need Singleton in Python. We need singleton just because we need some static data and static functions. In Python we can simply use following code:
def InitStaticData(data):
pass
class Global(object):
""" A global class to store static data."""
__private_data = xxx
static_data = InitStaticData(__private_data) //assume it's a list.
def ReadStaticData(arg):
return Global.StaticData[arg]
The Global class itself will be initialized for one time. And we can define related static function in the module. The only thing we should do is: do not use __init__ of Global.
Keep in mind: in Python, each module is itself an object without having to define an explicit class.
Thursday, February 14, 2008
Singleton in Python
发表者
fairfox
位置在:
11:27 PM
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment