简体   繁体   English

名称的Python id和'​​__main__'不同

[英]Python id of name and '__main__' different

Normally if I assign a variable some value, and then check their ids, I expect them to be the same, because python is essentially just giving my object a "name". 通常,如果我为变量分配一些值,然后检查它们的ID,我希望它们是相同的,因为python实际上只是给我的对象一个“名称”。 This can be seen in the below code: 这可以在下面的代码中看到:

>>> a = 3
>>> id(a)
19845928
>>> id(3)
19845928

The problem is when I perform the same with " name " 问题是当我用“ 名字 ”执行相同的操作时

>>> __name__
'__main__'
>>> id(__name__)
19652416
>>> id('__main__')
19652448

How can there ids be different, shouldn't they be the same? 那些ID怎么会有所不同,它们不应该是一样的吗? Because __name__ should also be just a reference. 因为__name__也应该只是一个引用。

id() gives essentially the memory pointer to the data. id()基本上给出了数据的内存指针。 Although strings are immutable, they are not guaranteed to be interned. 虽然字符串是不可变的,但不保证它们是实例化的。 This means that some strings with equal values have different pointers. 这意味着一些具有相同值的字符串具有不同的指针。

For integers (especially small ones), the pointers will be the same, so your 3 example works fine. 对于整数(特别是小整数),指针将是相同的,所以你的3例子工作正常。


@KartikAnand: The way you're checking for 'same object' is valid, although the usual way is to use x is y . @KartikAnand:你检查“同一个对象”的方式是有效的,尽管通常的方法是使用x is y The problem is that they are not the same object, and not guaranteed to be. 问题是它们不是同一个对象,并不能保证。 They simply have the same value. 它们只是具有相同的价值。 Note that when you do "__main__" you're creating a new object. 请注意,当您执行"__main__"您将创建一个新对象。 Sometimes python does a nice optimization and re-uses a previously-created string of the same value, but it doesn't have to. 有时,Python做一个很好的优化和重新使用相同的值的先前创建的字符串,但它不就得了。


Kartik's goal is to "verify that assignment is in a way reference and objects are not created on the fly". Kartik的目标是“验证赋值是否以某种方式引用,而对象不是即时创建的”。 To do this, avoid creating new objects (no string literals). 为此,请避免创建新对象(无字符串文字)。

>>> __name__
'__main__'
>>> x = __name__
>>> id(__name__)
3078339808L
>>> id(x)
3078339808L
>>> __name__ is x
True

Just because two strings have the same value, it does not follow that they are the same object. 仅仅因为两个字符串具有相同的值,并不意味着它们是同一个对象。 This is completely expected behaviour. 这是完全预期的行为。

In Python, small integers are "pooled", so that all small integer values point to the same object. 在Python中,小整数被“合并”,因此所有小整数值都指向同一个对象。 This is not necessary true for strings. 对于字符串,这不是必需的。

At any rate, this is an implementation detail that should not be relied upon. 无论如何,这是一个不应该依赖的实现细节。

What you are running in to here is the fact that primitives are pseudo (or real) singletons in Python. 你在这里运行的是基元是Python中的伪(或真实)单例的事实。 Further, looking at strings clouds the issue because when strings are interned, value and id become synonymous as a side effect, so some strings with value matches will have id matches and others won't. 此外,查看字符串会使问题浮现,因为当字符串被中断时,value和id会成为副作用的同义词,因此一些值匹配的字符串将具有id匹配而其他字符串则不会。 Try looking at hand-built objects instead, as then you control when a new instance is created, and id vs value becomes more explicitly clear. 尝试查看手工构建的对象,然后控制何时创建新实例,并且id vs value变得更明确。

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

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