Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python-为什么在方法中没有定义这个类变量?_Python_Variables_Scope - Fatal编程技术网

Python-为什么在方法中没有定义这个类变量?

Python-为什么在方法中没有定义这个类变量?,python,variables,scope,Python,Variables,Scope,我有一个python应用程序,如下所示: global_counter = 0 connections = {} class SocketHandler(): currentid = 0 def open(self): global global_counter global connections currentid = global_counter global_counter += 1 con

我有一个python应用程序,如下所示:

global_counter = 0
connections = {}

class SocketHandler():
    currentid = 0
    def open(self):
        global global_counter
        global connections
        currentid = global_counter
        global_counter += 1
        connections[currentid] = self
        print "WebSocket " + str(currentid) + " opened"

    def on_close(self):
        global connections
        print "WebSocket " + str(currentid) + " closed"
        del connections[currentid]
我得到了一个错误:

NameError: global name 'currentid' is not defined

在我打印的“打开”和“打开/关闭”行中,我正在打开/关闭连接。我在类中定义了它,为什么它不在范围中。另外,我读到过使用全局变量是不好的,但我看不到解决这个问题的方法。有人能指出我该做什么吗?谢谢。

currentid
应该是
self.currentid
,因为它是一个类变量。

currentid
应该是
self.currentid
,因为它是一个类变量。

currentid
是实例属性,所以使用
self.currentid
,而不是
currentid

def on_close(self):
        global connections
        print "WebSocket " + str(self.currentid) + " closed"
        del connections[self.currentid]

currentid
是实例属性,因此使用
self.currentid
而不是
currentid

def on_close(self):
        global connections
        print "WebSocket " + str(self.currentid) + " closed"
        del connections[self.currentid]

在Python中,您没有对方法内部属性的隐式访问权

行中的一个简单名称,如
currentid

del connections[currentid]
在尝试全局模块作用域之前,始终先在本地函数作用域中查找名称,然后在每个封闭函数作用域中查找名称(最后再查找内置函数)
currentid
是一个类属性,在这些作用域中找不到它

要在Python中查找属性,您总是需要指定要查找的对象。尽管查找协议意味着对象本身不必具有属性;属性查找将退回到指定对象的类(以及基类,如果涉及继承)

因此,这将起作用:

del connections[self.currentid]
但是,我认为您的代码的其余部分也不是您所认为的那样。
open
方法中的此行:

currentid = global_counter
不设置
SocketHandler
对象的
currentid
属性。分配给裸名称总是分配给局部变量,除非您明确声明它为
global
(您似乎知道这一点,因为您使用了
global
关键字)。因此在
open
方法中,
currentid
是一个局部函数变量;它的值在
open
方法的末尾丢失

事实上,您的
SocketHandler
对象根本没有
currentid
属性(除非您还有更多代码没有显示给我们)。将
currentid=0
放在类块中不会为所有
SocketHandler
实例提供
currentid
属性。它为
SocketHandler
本身提供了一个属性
currentid
;这就像
def open(self):
块在类对象上创建
open
属性(存储函数),而不是在每个实例上

on\u close
方法中读取
self.currentid
将无法在对象
self
中找到
currentid
属性,因此Python将查看
self
类,它是
SocketHandler
。该对象确实有一个
currentid
值,因此读取
self.currentid
的结果将是
0
,无论您以前是否在该
SocketHandler
上运行过
open

如果要将
currentid
作为实例变量存储在每个
SocketHandler
中,则
open
中的行需要是:

self.currentid = global_counter

这将分配给
self
引用的对象的
currentid
属性。然后,您还需要将方法中对
currentid
的所有其他引用更改为
self.currentid

在Python中,您没有对方法内部属性的隐式访问权

行中的一个简单名称,如
currentid

del connections[currentid]
在尝试全局模块作用域之前,始终先在本地函数作用域中查找名称,然后在每个封闭函数作用域中查找名称(最后再查找内置函数)
currentid
是一个类属性,在这些作用域中找不到它

要在Python中查找属性,您总是需要指定要查找的对象。尽管查找协议意味着对象本身不必具有属性;属性查找将退回到指定对象的类(以及基类,如果涉及继承)

因此,这将起作用:

del connections[self.currentid]
但是,我认为您的代码的其余部分也不是您所认为的那样。
open
方法中的此行:

currentid = global_counter
不设置
SocketHandler
对象的
currentid
属性。分配给裸名称总是分配给局部变量,除非您明确声明它为
global
(您似乎知道这一点,因为您使用了
global
关键字)。因此在
open
方法中,
currentid
是一个局部函数变量;它的值在
open
方法的末尾丢失

事实上,您的
SocketHandler
对象根本没有
currentid
属性(除非您还有更多代码没有显示给我们)。将
currentid=0
放在类块中不会为所有
SocketHandler
实例提供
currentid
属性。它为
SocketHandler
本身提供了一个属性
currentid
;这就像
def open(self):
块在类对象上创建
open
属性(存储函数),而不是在每个实例上

on\u close
方法中读取
self.currentid
将无法在对象
self
中找到
currentid
属性,因此Python将查看
self
类,它是
SocketHandler
。该对象确实有一个
currentid
值,因此读取
self.currentid
的结果将为