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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12790114/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 11:52:30  来源:igfitidea点击:

Python - Why is this class variable not defined in the method?

pythonvariablesscope

提问by Lylax

I have a python application which is as follows:

我有一个 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]

I'm getting the error:

我收到错误:

NameError: global name 'currentid' is not defined

on the lines of "open" and "on_close" where I print that I am opening/closing the connection. I defined it in the class, why is it not in scope. Also, I have read that using global variables is bad, but I don't see a way around this. Can someone point out what I should do? Thanks.

在“打开”和“on_close”行上,我打印我正在打开/关闭连接。我在类中定义了它,为什么它不在范围内。另外,我已经读到使用全局变量是不好的,但我没有找到解决方法。有人可以指出我应该做什么吗?谢谢。

采纳答案by Ben

You don't have implicit access to attributes inside methods, in Python.

在 Python 中,您无法隐式访问方法内部的属性。

A bare name like currentidin the line:

一个像currentid在行中的裸名:

del connections[currentid]

always looks up a name in the local function scope, then in each enclosing function scope, before trying the global module scope (and then looks at built-ins as a last resort). currentidis a class attribute, which won't be found in any of those scopes.

在尝试全局模块作用域之前,总是在局部函数作用域中查找名称,然后在每个封闭的函数作用域中查找名称(然后将内置函数作为最后的手段)。currentid是一个类属性,在任何这些范围中都找不到。

To look up an attribute in Python you alwaysneed to specify an object in which to look. Though the lookup protocol means the object need not necessarily have the attribute itself; attribute lookup will fall back to the class of the object you specified (and the base classes, if inheritance is involved).

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

So this would work:

所以这会起作用:

del connections[self.currentid]

However, I don't think the rest of your code is doing what you think it is either. This line in the openmethod:

但是,我认为您的其余代码也没有按照您的想法行事。open方法中的这一行:

currentid = global_counter

doesn't set the currentidattribute of your SocketHandlerobject. Assigning to a bare name alwaysassigns to a local variable, unless you explicitly declare it global(you appear to be aware of this, since you've used the globalkeyword). So in the openmethod, currentidis a local function variable; its value is lost at the end of the openmethod.

不会设置对象的currentid属性SocketHandler。分配给一个裸名总是分配给一个局部变量,除非你明确声明它global(你似乎意识到这一点,因为你已经使用了global关键字)。所以在open方法中,currentid是一个局部函数变量;它的值在open方法结束时丢失。

In fact, your SocketHandlerobjects do not have a currentidattribute at all (unless there's more code you haven't shown us). Putting currentid = 0in the class block doesn't give all the SocketHandlerinstances a currentidattribute. It gives the SocketHandlerclass itselfan attribute currentid; this is just as the def open(self):block creates an openattribute (storing a function) on the class object, not on each individual instance.

事实上,您的SocketHandler对象根本没有currentid属性(除非您没有向我们展示更多代码)。把currentid = 0班上块不把所有的SocketHandler情况下一个currentid属性。它给SocketHandler本身一个属性currentid;这就像def open(self):open在类对象上创建一个属性(存储一个函数),而不是在每个单独的实例上。

Reading self.currentidin the on_closemethod will fail to find a currentidattribute in the object self, so Python will look at the class of selfwhich is SocketHandler. That object doeshave a currentidvalue, so the result of reading self.currentidwill be 0, whether or not you've previously run openon that SocketHandler.

self.currentidon_close方法会找不到currentid对象中的属性self,所以Python会查看是self哪个类SocketHandler。这个对象确实有一个currentid值,所以阅读的结果self.currentid将是0,不管是不是你以前运行openSocketHandler

If you meant to store the currentidas an instance variable in each SocketHandler, then the line in openwould need to be:

如果您打算将 存储currentid为 each 中的实例变量SocketHandler,则行中open需要是:

self.currentid = global_counter

This assigns to the currentidattribute of the object referred to by self. You would also then need to change all the other references to currentidin your methods to self.currentid.

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

回答by ronak

currentidshould be self.currentidsince it is a class variable.

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

回答by defuz

currentidis instance attribute, so use self.currentidinstead of currentid:

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

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