我的代码是:
class New(Server):
noOfCl = 0
def onConnect(self, socket):
print "Client connected"
print (noOfCl+=1)
Run Code Online (Sandbox Code Playgroud)
我收到以下错误: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)
Run Code Online (Sandbox Code Playgroud)
printPython中也不支持在调用函数/语句时进行就地更新.