Python:有两个对象是sam

2024-05-14 22:44:45 发布

您现在位置:Python中文网/ 问答频道 /正文

Possible Duplicate:
Assignment of objects and fundamental types

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

此代码打印为真。为什么?”is“只有当两个变量指向同一个对象时才返回True,在本例中,它们是具有相同值的不同对象。”=”将返回True,但“is”不应返回

但是,自从

b.reverse()
print a,b

打印[3,2,1][3,2,1],似乎就解释器而言,它们是同一个对象,对b的操作将自动在a上执行。再说一遍,为什么?我以前从没见过这样的事。


Tags: andof对象代码trueobjectsistypes
3条回答

它们实际上引用的是同一个对象。

试试这个:

a = [1,2,3]
b = a
print b is a
b[0] = 0
print b is a

你会看到a和b都改变了,仍然是一样的。

当您执行a = [1, 2, 3]操作时,您将名称a绑定到列表对象。当您执行b = a时,您将名称b绑定到任何a上—在本例中是list对象。所以,他们是一样的。。。一个对象可以有多个名称。值得一读Python Data Model

如果您想制作listobj的副本,那么可以查看b = a[:]来使用slice创建浅副本,或者copy.copy来创建浅副本(应该在任意对象上工作),或者copy.deepcopy来创建奇怪的深副本。

您还将注意到CPython中一些令人惊讶的东西,它缓存短字符串/小整数。。。

>>> a = 4534534
>>> b = a
>>> a is b
True
>>> b = 4534534
>>> a is b
False
>>> a = 1
>>> b = a
>>> a is b
True
>>> b = 1
>>> a is b
True
a = [81, 82, 83]
b = a
print(a is b) #prints True

这就是这里实际发生的情况:

enter image description here

比如说:

a = [81,82,83]
b = [81,82,83]

print(a is b) # False

print(a == b)  #True, as == only checks value equality

enter image description here

In [24]: import sys

In [25]: a=[1,2,3]

In [26]: sys.getrefcount(a) #number of references to [1,2,3] are 2
Out[26]: 2

In [27]: b=a       #now b also points to [1,2,3]

In [28]: sys.getrefcount(a)   # reference to [1,2,3] got increased by 1,
                              # as b now also points to [1,2,3]
Out[28]: 3

In [29]: id(a)
Out[29]: 158656524      #both have the same id(), that's why "b is a" is True

In [30]: id(b)
Out[30]: 158656524

何时使用^{}模块:

In [1]: a=[1,2,3]

In [2]: b=a

In [3]: id(a),id(b)        
Out[3]: (143186380, 143186380)   #both point to the same object

In [4]: b=a[:]                #now use slicing, it is equivalent to b=copy.copy(a)
                              # or b= list(a)    

In [5]: id(a),id(b)
Out[5]: (143186380, 143185260)     #as expected both now point to different objects
                                   # so now changing one will not affect other

In [6]: a=[[1,2],[3,4]]          #list of lists

In [7]: b=a[:]                   #use slicing

In [8]: id(a),id(b)            #now both point to different object as expected
                               # But what about the internal lists?
Out[8]: (143184492, 143186380)

In [11]: [(id(x),id(y)) for (x,y) in zip(a,b)]   #so internal list are still same objects
                                                 #so doing a[0][3]=5, will changes b[0] too
Out[11]: [(143185036, 143185036), (143167244, 143167244)]

In [12]: from copy import deepcopy            #to fix that use deepcopy

In [13]: b=deepcopy(a)

In [14]: [(id(x),id(y)) for (x,y) in zip(a,b)]    #now internal lists are different too
Out[14]: [(143185036, 143167052), (143167244, 143166924)]

有关详细信息:

In [32]: def func():
   ....:     a=[1,2,3]
   ....:     b=a
   ....:     
   ....:     

In [34]: import dis

In [35]: dis.dis(func)
  2           0 LOAD_CONST               1 (1)
              3 LOAD_CONST               2 (2)
              6 LOAD_CONST               3 (3)
              9 BUILD_LIST               3
             12 STORE_FAST               0 (a)   #now 'a' poits to [1,2,3]

  3          15 LOAD_FAST                0 (a)    #load the object referenced by a
             18 STORE_FAST               1 (b)    #store the object returned by a to b 
             21 LOAD_CONST               0 (None)
             24 RETURN_VALUE        

In [36]: def func1():
   ....:     a=[1,2,3]
   ....:     b=[1,2,3]
   ....:     
   ....:     

In [37]: dis.dis(func1)      #here both a and b are loaded separately
  2           0 LOAD_CONST               1 (1)
              3 LOAD_CONST               2 (2)
              6 LOAD_CONST               3 (3)
              9 BUILD_LIST               3
             12 STORE_FAST               0 (a)

  3          15 LOAD_CONST               1 (1)
             18 LOAD_CONST               2 (2)
             21 LOAD_CONST               3 (3)
             24 BUILD_LIST               3
             27 STORE_FAST               1 (b)
             30 LOAD_CONST               0 (None)
             33 RETURN_VALUE   

相关问题 更多 >

    热门问题