python如何管理类内的变量?
假设我有以下两个 python 文件。
foo.py
class Foo:
def __init__(self):
self.my_var = 0
def print_var(self):
print(f"self.my_var: {self.my_var}")
print(f"self.my_var address: {hex(id(self.my_var))}")
def set_var(self, v):
self.my_var = v
test_foo.py
import foo
f1 = foo.Foo()
f2 = foo.Foo()
print(f"\n=========== first print ===========")
f1.print_var()
f2.print_var()
f1.set_var(3)
print(f"\n=========== second print ===========")
f1.print_var()
f2.print_var()
当我运行 python3 test_foo.py
时,我得到以下结果:
=========== first print ===========
self.my_var: 0
self.my_var address: 0x7ffb7477a180 # Why do f1's and f2's `self.my_var` refer to the same instance?
self.my_var: 0
self.my_var address: 0x7ffb7477a180
=========== second print ===========
self.my_var: 3
self.my_var address: 0x7ffb7477a1e0
self.my_var: 0
self.my_var address: 0x7ffb7477a180
我很好奇为什么在第一个print,f1 和 f2 的 self.my_var 引用同一个实例。 我的意思是它们都引用位于 0x7ffb7477a180
的 vairable。
我预计他们会提到不同的实例。
这是我的预期输出,尽管我知道这是错误的。
=========== first print ===========
self.my_var: 0
self.my_var address: 0x7ffb7477a1e0
self.my_var: 0
self.my_var address: 0x7ffb7477a180
=========== second print ===========
self.my_var: 3
self.my_var address: 0x7ffb7477a1e0
self.my_var: 0
self.my_var address: 0x7ffb7477a180
python 有任何文档解释它如何管理类内的变量吗?
Let's say I have the following two python files.
foo.py
class Foo:
def __init__(self):
self.my_var = 0
def print_var(self):
print(f"self.my_var: {self.my_var}")
print(f"self.my_var address: {hex(id(self.my_var))}")
def set_var(self, v):
self.my_var = v
test_foo.py
import foo
f1 = foo.Foo()
f2 = foo.Foo()
print(f"\n=========== first print ===========")
f1.print_var()
f2.print_var()
f1.set_var(3)
print(f"\n=========== second print ===========")
f1.print_var()
f2.print_var()
When I run python3 test_foo.py
, I get the following result:
=========== first print ===========
self.my_var: 0
self.my_var address: 0x7ffb7477a180 # Why do f1's and f2's `self.my_var` refer to the same instance?
self.my_var: 0
self.my_var address: 0x7ffb7477a180
=========== second print ===========
self.my_var: 3
self.my_var address: 0x7ffb7477a1e0
self.my_var: 0
self.my_var address: 0x7ffb7477a180
I'm curious about why in the first print, f1's and f2's self.my_var
refer to the same instance.
I mean they all refer to the vairable located at 0x7ffb7477a180
.
I expected they would refer to different instances.
Here's my expected output, though I know it's wrong.
=========== first print ===========
self.my_var: 0
self.my_var address: 0x7ffb7477a1e0
self.my_var: 0
self.my_var address: 0x7ffb7477a180
=========== second print ===========
self.my_var: 3
self.my_var address: 0x7ffb7477a1e0
self.my_var: 0
self.my_var address: 0x7ffb7477a180
Does python have any document explaining how it manages variables inside a class?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
原因是小整数缓存。 Python 会缓存小整数,即 -5 到 256 之间的整数。这些数字使用得非常频繁,因此最好让这些对象可用,从而提高性能。所以这些整数将在启动时被分配。然后,每次引用一个对象时,您都会引用一个已经存在的对象。这就是原因,您会看到值为
0
的变量的相同引用。然而,这对于 CPython 来说是正确的,我读到它与其他实现有一些差异。请参阅此答案以获得更广泛的解释
The reason is Small Integer Caching. Python caches small integers, which are integers between -5 and 256. These numbers are used so frequently that it’s better for performance to already have these objects available. So these integers will be assigned at startup. Then, each time you refer to one, you’ll be referring to an object that already exists. This is the reason, you see the same reference for variables with value
0
.However, this is true to CPython, I have read it has some differences based on the other implementations. Refer this answer for a broader explanation