引用如何在python整数vs列表中工作?列表中两个值都相同,为什么整数部分中的值不为2? [英] how reference works in python integer vs list ? in list both value are same, why is in integer section a value is not 2?

查看:61
本文介绍了引用如何在python整数vs列表中工作?列表中两个值都相同,为什么整数部分中的值不为2?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

列出参考附加代码

a = [1,2,3,4,5]
b = a
b.append(6)
print(a)
print(b)
#ans:
[1,2,3,4,5,6]
[1,2,3,4,5,6]

int中的整数引用

a = 1
b = a
b +=1
print(a)
print(b)
#ans:
1
2

参考如何在python整数vs列表中工作?列表中两个值都相同,为什么整数部分中的值不为2?

how reference works in python integer vs list ? in list both value are same, why is in integer section a value is not 2 ?

推荐答案

在Python中,一切都是对象.根据地址(指针)的名称="nofollow noreferrer">文档.

In Python, everything is an object. Everything is a name for an address (pointer) per the docs.

在该页面上,您可以向下滚动并找到以下内容:

On that page you can scroll down and find the following:

数字对象是不可变的;一旦创造了价值,就永远不会改变

Numeric objects are immutable; once created their value never changes

在下面,您将看到定义的 int 类型,因此您的第二个示例可以正常使用.

Under that you'll see the int type defined, so it makes perfect sense your second example works.

在同一页面的顶部,您将找到以下内容:

On the top of the same page, you'll find the following:

每个对象都有一个标识,一个类型和一个值.创建对象后,其身份就永远不会改变;您可能会认为它是对象在内存中的地址.

Every object has an identity, a type and a value. An object’s identity never changes once it has been created; you may think of it as the object’s address in memory.

Python的行为就像C和Java,因为您无法重新分配指向名称的指针所在的位置.像Java一样,Python也是按值传递的,并且没有按引用传递的语义.

Python behaves just like C and Java in that you cannot reassign where the pointer to a name points. Python, like Java, is also pass-by-value and doesn't have a pass-by-reference semantic.

看你的第一个例子:

>>> a = 1
>>> hex(id(a))
'0x7ffdc64cd420'
>>> b = a + 1
>>> hex(id(b))
'0x7ffdc64cd440'
>>> print(a)
1
>>> print(b)
2

在此显示,操作 b = a + 1 a 保留在 1 ,而 b 为现在 2 .这是因为 int 是不可变的,指向值 1 的名称将始终指向相同的地址:

Here it is shown that the operation b = a + 1 leaves a at 1 and b is now 2. That's because int is immutable, names that point to the value 1 will always point to the same address:

>>> a = 1
>>> b = 2
>>> c = 1
>>> hex(id(a))
'0x7ffdc64cd420'
>>> hex(id(b))
'0x7ffdc64cd440'
>>> hex(id(c))
'0x7ffdc64cd420'

现在,这仅适用于-5 到 256 的值-api/long.html#c.PyLong_FromLong"rel =" nofollow noreferrer> C实现,因此除此以外,您还可以获得新的地址,但是上面显示的可变性仍然适用.我已经向您展示了共享内存地址的原因.在同一页面上,您将找到以下内容:

Now this only holds true for the values of -5 to 256 in the C implementation, so beyond that you get new addresses, but the mutability shown above holds. I've shown you the sharing of memory addresses for a reason. On the same page you'll find the following:

类型几乎影响对象行为的所有方面.甚至对象标识的重要性在某种意义上也受到影响:对于不可变类型,计算新值的操作实际上可能返回对具有相同类型和值的任何现有对象的引用,而对于可变对象,则不允许这样做.例如,在a = 1之后;b = 1,取决于实现,a和b可以或可以不使用值1引用同一对象,但是在c = []之后;d = [],保证c和d引用两个不同的,唯一的,新创建的空列表.(请注意,c = d = []为c和d分配了相同的对象.)

Types affect almost all aspects of object behavior. Even the importance of object identity is affected in some sense: for immutable types, operations that compute new values may actually return a reference to any existing object with the same type and value, while for mutable objects this is not allowed. E.g., after a = 1; b = 1, a and b may or may not refer to the same object with the value one, depending on the implementation, but after c = []; d = [], c and d are guaranteed to refer to two different, unique, newly created empty lists. (Note that c = d = [] assigns the same object to both c and d.)

所以你的例子:

>>> a = [1, 2, 3, 4, 5]
>>> hex(id(a))
'0x17292e1cbc8'
>>> b = a
>>> hex(id(b))
'0x17292e1cbc8'

我应该能够在这里停下来,显而易见, a b 都引用了内存中位于地址 0x17292e1cbc8 的同一对象.那是因为上面就像说:

I should be able to stop right here, its obvious that both a and b refer to the same object in memory at address 0x17292e1cbc8. Thats because the above is like saying:

# Lets assume that `[1, 2, 3, 4, 5]` is 0x17292e1cbc8 in memory
>>> a = 0x17292e1cbc8
>>> b = a
>>> print(b)
'0x17292e1cbc8'

又长又瘦?您只需分配一个指向新名称的指针,但两个名称都指向内存中的同一对象!注意:这与

Long and skinny? You're simply assigning a pointer to a new name, but both names point to the same object in memory! Note: This is not the same as a shallow copy because no external compound object is made.

这篇关于引用如何在python整数vs列表中工作?列表中两个值都相同,为什么整数部分中的值不为2?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆