[英]Python - variable referenced before assignment
我的代码是:
class New(Server):
noOfCl = 0
def onConnect(self, socket):
print "Client connected"
print (noOfCl+=1)
我收到以下错误: UnboundLocalError: local variable 'noOfCl' referenced before assignment.
据我了解,我在引用noOfCl之前先声明了它。 有人对我在做什么错有任何想法吗? 谢谢
由于noOfCl
是类变量,因此需要在类名之前添加前缀。
class New(Server):
noOfCl = 0
def onConnect(self, socket):
print "Client connected"
New.noOfCl+=1
print(New.noOfCl)
此外,Python不支持调用print
函数/语句时的就地更新。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.