为什么 Python 中每个新对象 refs 都会增加 2?
对我来说有点奇怪的是,在定义新对象后,交互环境中的 refs 编号增加了 2。我只创建了一个对象,不是吗?
>>> v
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'v' is not defined
[41830 refs]
>>> v = "v"
[41832 refs]
It is a little weird to me that the refs number in the interactive environment increases 2 after a new object is defined. I created only one object, isn't it?
>>> v
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'v' is not defined
[41830 refs]
>>> v = "v"
[41832 refs]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的作业是通过在
globals()
字典中创建一个条目来完成的,该条目以v
作为键,以"v"
作为值。这是两个引用(一个用于键,一个用于值),尽管在本例中它们可能都引用相同的字符串"v"
。Your assignment worked by creating an entry in the
globals()
dictionary that hasv
as a key and"v"
as a value. That's two references (one for the key and one for the value) although in this case they probably both refer to the same string"v"
.