Python - 为什么这个类变量没有在方法中定义? [英] Python - Why is this class variable not defined in the method?

查看:51
本文介绍了Python - 为什么这个类变量没有在方法中定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a python application which is as follows:

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

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

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.

推荐答案

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

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

一个像 currentid 这样的裸名:

A bare name like currentid in the line:

del connections[currentid]

总是在局部函数作用域中查找名称,然后在每个封闭的函数作用域中查找名称,然后再尝试全局模块作用域(然后将内置函数作为最后的手段).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). currentid is a class attribute, which won't be found in any of those scopes.

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

To look up an attribute in Python you always need 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).

所以这行得通:

del connections[self.currentid]

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

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

currentid = global_counter

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

doesn't set the currentid attribute of your SocketHandler object. Assigning to a bare name always assigns to a local variable, unless you explicitly declare it global (you appear to be aware of this, since you've used the global keyword). So in the open method, currentid is a local function variable; its value is lost at the end of the open method.

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

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

on_close方法中读取self.currentid将无法在对象self中找到currentid属性,所以Python会查看self的类,也就是SocketHandler.该对象确实有一个currentid值,所以读取self.currentid的结果将是0,无论是不是您之前在该 SocketHandler 上运行过 open.

Reading self.currentid in the on_close method will fail to find a currentid attribute in the object self, so Python will look at the class of self which is SocketHandler. That object does have a currentid value, so the result of reading self.currentid will be 0, whether or not you've previously run open on that SocketHandler.

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

If you meant to store the currentid as an instance variable in each SocketHandler, then the line in open would need to be:

self.currentid = global_counter

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

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

这篇关于Python - 为什么这个类变量没有在方法中定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆