简体   繁体   English

为什么变量在 Python 中按值传递时具有相同的 id

[英]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()


Output: 输出:

 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.

相关问题 为什么具有相同值的可变对象在Python中具有不同的ID - why mutable objects having same value have different id in Python 如何在 Python 中为两个或多个变量分配相同的类值,使它们不在同一个 ID 下? - How do I assign the same class value to two or more variables in Python, so they are not under the same ID? 在Python中,赋值运算符在类方法定义中作为默认值传递时是否访问类或实例变量? - In Python, do assignment operators access class or instance variables when passed as a default value in a class method definition? 当相同的值分配给两个不同的变量时,为什么浮点值的 id 不同? - Why the id of float values are different when the same value is assigned to two different variables? Python 中的两个变量具有相同的 id,但不是列表或元组 - Two variables in Python have same id, but not lists or tuples 为什么同一类的不同对象的方法具有相同的ID? - Why do methods of different objects of same class have same id? 为什么多处理中的新对象具有相同的 id? - Why do new objects in multiprocessing have the same id? 为什么多个 celery 工作人员似乎具有相同的 greenlet ID? - Why do multiple celery workers appear to have the same greenlet ID? 为什么不同的类实例在 python 中具有相同的 id? - Why the diffent class instance have same id in python? 为什么复制的对象具有与以前在Python中复制的对象相同的ID? - Why copied objects have the same id as previously copied ones in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM