【发布时间】:2015-07-30 13:47:00
【问题描述】:
我正在尝试在 Python (2.7) 中实现 Singleton pattern。
我已经阅读了有关实现的严重帖子(1、2、3、4),我想编写自己的版本。 (我理解的版本。我是 Python 新手。)
所以我正在使用一种方法创建单例,该方法将创建我的单个对象本身,该对象将在每次 Singleton.Instance() 调用时返回。
但是错误信息总是一样的:
Traceback (most recent call last):
File "./test4.py", line 24, in <module>
print id(s.Instance())
File "./test4.py", line 15, in Instance
Singleton._instance = Singleton._creator();
TypeError: unbound method foobar() must be called with Singleton instance as first argument (got nothing instead)
我在这里滚动:
class Singleton(object):
_creator = None
_instance = None
def __init__(self, creator):
if Singleton._creator is None and creator is not None:
Singleton._creator = creator
def Instance(self):
if Singleton._instance is not None:
return Singleton._instance
Singleton._instance = Singleton._creator();
return Singleton._instance;
def foobar():
return "foobar"
s = Singleton( foobar )
print id(s.Instance())
这是为什么呢?更具体地说:如何在 Python 中调用存储在类变量中的 def 方法?
【问题讨论】:
-
我建议使用可以应用于任何类的装饰器(例如定义自己的
@singleton) -
装饰器非常适合用于生产代码,但对于刚刚编写“我理解的版本。我是 Python 新手”的人来说,最初的代码尝试可能不是那么好。
标签: python python-2.7 design-patterns singleton