简体   繁体   English

列表的变量是 Python 中的 object 吗?

[英]Is list's variable is an object in Python?

a = [1,2,3]
b = [1,2,3]

I understand a is b is false but i can't understand a is [1,2,3] is false我知道a is b是假的,但我不明白a is [1,2,3]是假的

I learned variable is like nickname for objects like x = 2 and id(x) == id(2)我了解到变量就像x = 2id(x) == id(2)这样的对象的昵称

but id(a) is not as same as id(b) ...id(a)id(b)不同...

In this case, a is an object?在这种情况下,a 是 object? not a variable?不是变量?

Variables are references to objects.变量是对对象的引用。 a does not reference the same object as b . a没有引用与b相同的 object 。 Even though the two objects are the same they have unique addresses in memory and do not depend on each other.尽管这两个对象相同,但它们在 memory 中具有唯一的地址,并且不相互依赖。

>>> a = b = [1,2,3]
>>> c = [1,2,3]
>>> print(a is b)
True
>>> print(a is c or b is c)
False
>>> a.remove(1)
>>> print(a)
[2, 3]
>>> print(b)
[2, 3]
>>> print(c)
[1, 2, 3]

In the case of the x = 2 and id(x) == id(2) integers are immutable and in CPython id is simply the location of an object in memory.x = 2id(x) == id(2)的情况下,整数是不可变的,而在 CPython 中, id只是 memory 中 object 的位置。 Integers are always the same so storing the same integer several times at different addresses would be a waste of memory.整数总是相同的,因此在不同的地址多次存储相同的 integer 将浪费 memory。

However, in general DO NOT use integers with is operator as it can lead to different results across different python implementations.但是,通常不要将整数与is运算符一起使用,因为它可能导致不同 python 实现的结果不同。

Everything in Python is an object. Python 中的所有内容都是 object。 a is b evaluates to false because, as you know, they are not the same object. a is b评估为假,因为如您所知,它们与 object 不同。 They are both instances of the List class, therefore having different locations in memory, and being completely separate in every way except their class structure, or blueprint, as you can think of it.它们都是List class 的实例,因此在 memory 中具有不同的位置,并且除了它们的 class 结构或蓝图外,它们在各个方面都是完全独立的。 id(a) and id(b) will reflect this, they are not the same object and will therefore not share an ID. id(a)id(b)将反映这一点,它们不是同一个 object,因此不会共享一个 ID。 This is a reference to their location in memory, and while IDs are not referenced pointers, they are similar in that they describe their not being the same.这是对它们在 memory 中位置的引用,虽然 ID不是引用的指针,但它们的相似之处在于它们描述了它们的不同之处。

>>> a = [1, 2, 3]
>>> b = [1, 2, 3]
>>> id(a) == id(b)
False
>>> # the below is an example, every session will result in a different ID,
>>> # as it's being stored at a unique mem location each time
>>> id(a)
2770873780160
>>> id(b)
2770873412800
>>> id(a) - id(b)  # simply to show the difference in location.
367360

double equals vs is in python 双等于 vs 在 python

https://www.tutorialspoint.com/difference-between-and-is-operator-in-python https://www.tutorialspoint.com/difference-between-and-is-operator-in-python

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果变量在列表 B 中,则删除列表 A 中的变量,Python - Remove Variable(s) in List A if Variable(s) is/are in List B, Python 如何使用变量作为对象的名称:Python - How to use a variable as an object's name: Python Python:变量存储对象的地址吗? - Python: Is variable stores the object's address? Python:TypeError:“列表”对象不可在全局变量上调用 - Python: TypeError: 'list' object is not callable on global variable Python,将变量的名称添加到列表/字典 - Python, add variable's names to a list/dict Python:带有变量参数列表的对象赋值 - Python: Object assignment with variable argument list 在python django中,如何打印出对象的内省? 该对象的所有公共方法列表(变量和/或函数)? - In python django how do you print out an object's introspection? The list of all public methods of that object (variable and/or functions)? 将Python对象的变量作为函数调用,而不传递调用对象 - Call Python object's variable as a function without passing the calling object 如何在html中提取python的列表变量的值? - How to extract python's list variable's value in html? 从另一个对象的变量设置对象的变量时奇怪的 Python 行为 - Weird Python behavior when setting an object's variable from another object's variable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM