[英]Python class variable name vs __name__
I'm trying to understand the relationship between the variable a Python class object is assigned to and the __name__
attribute for that class object. 我试图理解分配Python类对象的变量与该类对象的
__name__
属性之间的关系。 For example: 例如:
In [1]: class Foo(object):
...: pass
...:
In [2]: Foo.__name__ = 'Bar'
In [3]: Foo.__name__
Out[3]: 'Bar'
In [4]: Foo
Out[4]: __main__.Bar
In [5]: Bar
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-5-962d3beb4fd6> in <module>()
----> 1 Bar
NameError: name 'Bar' is not defined
So it seems like I have changed the __name__
attribute of the class but I can't refer to it by that name. 所以看起来我已经改变了类的
__name__
属性,但我不能用它来引用它。 I know this is a bit general but could someone explain the relationship between Foo
and Foo.__name__
? 我知道这有点普遍,但有人可以解释
Foo
和Foo.__name__
之间的关系Foo.__name__
?
It's simple. 这很简单。 There is no relationship at all.
完全没有关系。
When you create a class a local variable is created with name you used, pointing at the class so you can use it. 创建类时,将使用您使用的名称创建局部变量,指向该类以便您可以使用它。
The class also gets an attribute __name__
that contains the name of that variable, because that's handy in certain cases, like pickling. 该类还获取一个属性
__name__
,该属性包含该变量的名称,因为在某些情况下这很方便,比如酸洗。
You can set the local variable to something else, or change the __name__
variable, but then things like pickling won't work, so don't do that. 你可以将局部变量设置为其他东西,或者更改
__name__
变量,但是像酸洗这样的东西将不起作用,所以不要这样做。
__name__
is mere self-identification, in oder to know what type an instance of it really is. __name__
仅仅是自我识别, __name__
得知道它的实例是什么类型。
The other thing is the way it can be accessed with. 另一件事是它可以被访问的方式。 That can vary if you re-assign it.
如果您重新分配,这可能会有所不同。
They both are assigned at the time you define the class. 它们都是在您定义类时分配的。
It works the same way with functions: if you def
them, they get assigned to the given name and they get the respective __name__
attribute. 它的工作原理与功能,以同样的方式:如果你
def
他们,他们会被分配到给定的名字和他们得到相应的__name__
属性。
OTOH, if you have a lambda
function, it gets a __name__
attribute of <lambda>
, because it doesn't know the name it gets assigned to. OTOH,如果你有一个
lambda
函数,它会获得<lambda>
的__name__
属性,因为它不知道它被分配给的名称。
Short version 简洁版本
class Foo(object): pass
creates a class and assigns it to local name Foo
. class Foo(object): pass
创建一个类并将其分配给本地名称Foo
。
Foo.__name__ = 'Bar'
assigns a new value to attribute __name__
. Foo.__name__ = 'Bar'
为属性__name__
分配一个新值。 The enclosing scope is not affected. 封闭范围不受影响。
Long version 长版
The class statement creates a class and assigns to the name provided in the local scope. class语句创建一个类并分配给本地范围中提供的名称。 When creating a class Python tells the class the name it was created with by assigning it to the class's
__name__
attribute. 创建类时,Python通过将类指定给类的
__name__
属性来告诉类创建它的__name__
。
Assigning to a class's attribute does not introduce a name into the local scope. 分配给类的属性不会在本地范围中引入名称。 Therefore any changes to attributes (such as
__name__
) do not affect the enclosing scope. 因此,对属性(例如
__name__
)的任何更改都不会影响封闭范围。
You need to keep in mind that in python a class is just an object like any other. 你需要记住,在python中,一个类只是一个像任何其他对象一样的对象。 It wouldn't make sense for an object to contain an attribute that was linked to a variable that refers to the object, because there could be any number of variable names referring to it.
对象包含链接到引用该对象的变量的属性是没有意义的,因为可能有任意数量的变量名称引用它。 Any time you write an assignment (
Bar = Foo
) or pass the object to a function, you have a new reference. 每次编写赋值(
Bar = Foo
)或将对象传递给函数时,都会有一个新的引用。 Naturally all objects must be independent of how they are referenced. 当然,所有对象必须独立于它们的引用方式。
__name__
is simply a piece of information attached to the class object, which happens to be the same as the variable name it's initially assigned to. __name__
只是附加到类对象的一条信息,它恰好与它最初分配给它的变量名相同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.