[英]Why do variables have same id when they are passed by value in Python
I have been studying functions in python and how the variables are passed by values and not reference since its a lot safer.我一直在研究 python 中的函数以及变量如何通过值传递而不是引用,因为它更安全。 if that is the way it is then why do the variable passed around have the same ID.I initially used
x=3
but then I read that how python caches variables from -5 to 256
so I used 500 but it still shows the same id.如果是这样,那么为什么传递的变量具有相同的 ID。我最初使用
x=3
但后来我读到 python 如何缓存从-5 to 256
变量,所以我使用了 500 但它仍然显示相同的 ID . If they have the same doesn't it mean that it is the same object passed around?如果它们相同,是否意味着它是传递的同一个对象?
x = 500
print(id(x))
def cat(a):
print(id(a))
def main():
a = x
print(id(a))
cat(a)
if __name__ == '__main__': main()
4511355632 4511355632 4511355632
I have been studying functions in python and how the variables are passed by values and not reference since its a lot safer
我一直在研究 python 中的函数以及变量如何通过值传递而不是引用,因为它更安全
I don't know where you read this, but that's just complete bullshit.我不知道你在哪里读到的,但这完全是胡说八道。
if that is the way it is
如果是这样的话
It isn't.不是。
then why do the variable passed around have the same ID.
那么为什么传递的变量具有相同的 ID。
Because they point to the same object, obviously.因为它们指向同一个对象,很明显。
I initially used x=3 but then I read that how python caches variables from -5 to 256
我最初使用 x=3 但后来我读到 python 如何缓存从 -5 到 256 的变量
That's a CPython implementation detail, and the exact values depend on the CPython version etc. But anyway, you can test this with any number you want, and actually just any type, the result will still be the same.这是一个 CPython 实现细节,确切的值取决于 CPython 版本等。但是无论如何,你可以用任何你想要的数字来测试它,实际上只是任何类型,结果仍然是一样的。
so I used 500 but it still shows the same id.
所以我使用了 500 但它仍然显示相同的 ID。 If they have the same doesn't it mean that it is the same object passed around?
如果它们相同,是否意味着它是传递的同一个对象?
id(obj)
, by definition, returns the object's unique identifier (actually the memory address in CPython but that's also an implementation detail), so by definition, if two objects have the same id, then they are indeed the very same object. id(obj)
,根据定义,返回对象的唯一标识符(实际上是 CPython 中的内存地址,但这也是一个实现细节),因此根据定义,如果两个对象具有相同的 id,那么它们确实是同一个对象。
NB : "unique" meaning that for the current process, no other object will have the same id at the same time - once an object is garbage-collected, it's id can be reused. NB:“独特的”,这意味着当前进程,没有其他的对象将在同一时间同一ID -一旦对象被垃圾回收,它的ID可以重复使用。
FWIW, using a mutable object, it's quite easy to find out that it's not passed "by value": FWIW,使用可变对象,很容易发现它不是“按值”传递的:
def foo(lst):
lst.append(42)
answers = []
for i in range(10):
print("{} - before: {}".format(i, answers))
foo(answers)
print("{} - after: {}".format(i, answers))
0 - before: []
0 - after: [42]
1 - before: [42]
1 - after: [42, 42]
2 - before: [42, 42]
2 - after: [42, 42, 42]
3 - before: [42, 42, 42]
3 - after: [42, 42, 42, 42]
4 - before: [42, 42, 42, 42]
4 - after: [42, 42, 42, 42, 42]
5 - before: [42, 42, 42, 42, 42]
5 - after: [42, 42, 42, 42, 42, 42]
6 - before: [42, 42, 42, 42, 42, 42]
6 - after: [42, 42, 42, 42, 42, 42, 42]
7 - before: [42, 42, 42, 42, 42, 42, 42]
7 - after: [42, 42, 42, 42, 42, 42, 42, 42]
8 - before: [42, 42, 42, 42, 42, 42, 42, 42]
8 - after: [42, 42, 42, 42, 42, 42, 42, 42, 42]
9 - before: [42, 42, 42, 42, 42, 42, 42, 42, 42]
9 - after: [42, 42, 42, 42, 42, 42, 42, 42, 42, 42]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.