我试图用python制作一个玩具singleton来学习该语言的来龙去脉,但遇到了python如何工作的问题。我这样宣布班级
class ErrorLogger:
# Singleton that provides logging to a file
instance = None
def getInstance():
# Our singleton "constructor"
if instance is None :
print "foo"
当我叫它
^{pr2}$我明白了
File "/home/paul/projects/peachpit/src/ErrorLogger.py", line 7, in getInstance
if instance is None :
UnboundLocalError: local variable 'instance' referenced before assignment
这是怎么回事,实例不应该被静态地赋值为Null吗?正确的方法是什么?在
您必须使用
ErrorLogger
前缀来调用它,因为它是一个静态变量。在相关问题 更多 >
编程相关推荐