简体   繁体   English

为什么需要限定局部变量?

[英]Why do I need to qualify local variables?

In this class: 在本课程中:

class MyClass () :
    foo = 1

    @staticmethod
    def bar () :
        print MyClass.foo

Why do I need to qualify foo with MyClass ? 为什么我需要使用MyClass来限定foo (otherwise I get NameError: global name 'foo' is not defined . (否则我得到NameError: global name 'foo' is not defined

Isn't foo local to the class MyClass ? foo不是MyClass类的本地对象吗?

This is because Python's scope lookup order is LEGB (locals, enclosed function, global, builtin). 这是因为Python的范围查找顺序是LEGB(本地,封闭函数,全局,内置)。 More details in this answer . 此答案中有更多详细信息。 Python has an explicit class variable, which is the first argument of the method, typically named self . Python有一个显式的类变量,它是方法的第一个参数,通常称为self Normally one would access foo by using self.foo But in this case, the function is a static method, so it does not receive an explicit class variable, so there is no alternative way to access foo . 通常,可以使用self.foo访问foo ,但是在这种情况下,该函数是静态方法,因此它不会收到显式的类变量,因此没有替代方法可以访问foo Either remove the reference to foo or remove the @staticmethod decorator from the bar() and add self as the first argument of bar() . 删除对foo的引用或从bar()删除@staticmethod装饰器,然后将self添加为bar()的第一个参数。

You need to do that because the bar function is a static method. 您需要这样做,因为bar函数是静态方法。 This means you can call it without regarding an instance of the containing class. 这意味着您可以在不考虑包含类实例的情况下调用它。 IE you don't have to create an instance of the class to access that function. IE,您无需创建类的实例即可访问该功能。

You can read more about it - in the documentation 您可以在文档中了解更多信息

This is called class attribute which could be accessed directly by MyClass.foo , and owned by the class. 这称为类属性,可以由MyClass.foo直接访问,并由该类拥有。 It's not owned by the instances of the class 它不属于该类的实例

for self this is instance variable, each instance of a class has a new copy of the variables 对于self这是实例变量,类的每个实例都有一个变量的新副本

In Python, the concept of "local variables" really fully exists only in functions. 在Python中,“局部变量”的概念实际上完全存在于函数中。 A function (method) inside a class does not have implicit access to the class's (or instance's) scope; 类内部的函数(方法)不能隐式访问该类(或实例)的范围。 you must explicitly specify the object containing the desired attribute, ie, the class or the instance (by convention passed to the method as self ). 您必须显式指定包含所需属性的对象,即类或实例(按照惯例作为self传递给该方法)。 As to why it was designed that way... you'd have to ask Guido, but the Zen of Python says "explicit is better than implicit" so that might have something to do with it. 至于为什么这样设计……您不得不问Guido,但是Python的Zen说“显性比隐式更好”,因此可能与它有关。

Isn't foo local to the class MyClass? foo不是MyClass类的本地对象吗?

Actually, no. 其实没有 It's local to the class statement's body, which the bar function cannot access. 它在class语句的主体本地, bar函数无法访问。 Once the class object created and bound to MyClass , foo becomes an attribute of the class object (just like bar FWIW), but that's namespace, not scope. 一旦创建了类对象并将其绑定到MyClassfoo成为该类对象的属性(就像FWIW bar一样),但这是名称空间,而不是作用域。

Also and FWIW, Python's staticmethod dont access the class itself. 同样和FWIW一样,Python的staticmethod访问类本身。 If you want a method that needs to access the class, use a classmethod instead. 如果要使用需要访问该类的方法,请改用classmethod

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM