Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我认为每个对象都有一个id基于键。
id
>>> a = 10 >>> b = 20 >>> id(a) 1876869280 >>> id(b) 1876869440 >>> a,b=b,a >>> id(a) 1876869440 >>> id(b) 1876869280
当交换变量时,它们的 id 也会被交换。如果每个对象都有一个唯一的 id,那么为什么要交换 id?我认为交换id(a)后id(b)会一样。
id(a)
id(b)
如果每个对象都有一个唯一的 id,那么为什么要交换 id?
因为你交换了对象。对象不是a和b,它们是10和20。a并且b只是代码用来引用这些对象的名称。
a
b
10
20