
     1   wowpanda      2016-04-17 23:48:54 +08:00 via Android  楼主能贴出来在那些环境相同,哪些不同吗   |  
     2   Abirdcfly      2016-04-17 23:54:31 +08:00  没那么复杂         >>> (a,b) = (1,2) >>> print id((a,b)) 140320109531008 >>> (a,b) = ("string",2) >>> print id((a,b)) 140320109530936 上面的不一样. >>> (a,b) = (1,2);print id((a,b));(a,b) = ("string",2);print id((a,b)) 140320109531080 140320109531080 下面的一样. 为什么我就不清楚了..只能解释为解释器的优化.. 我之前问过一个类似的...回答那哥们也是从解释器优化说的..字节码显示是优化过了..为什么就不清楚了.. [When the Python interpreter deals with a .py file, is it different from dealing with a single statement?]( http://stackoverflow.com/questions/36474782/when-the-python-interpreter-deals-with-a-py-file-is-it-different-from-dealing) 有哪位大神路过看看呗?        |  
     4   itlynn   OP  |  
     5   wartime      2016-04-18 00:05:17 +08:00   id(...)  id(object) -> integer Return the identity of an object. This is guaranteed to be unique among simultaneously existing objects. (Hint: it's the object's memory address.) (a, b) 实际上是临时分配的一个变量,由于没有引用可能马上释放。 (1, 2) 和 ('string', 2) 如果凑巧在相同位置,看上去 id 值一样,实际上之前的已经释放,内容已经改变。 c = (a,b) = (1,2) print id(c) d = (a,b) = ("string",2) print id(d) 在 c 和 d 的值不变的情况下, id(c) id(d)值不变 (tuple 是 immutable)  |  
     6   glennq      2016-04-18 00:08:56 +08:00  一样是巧合,不一样才正常   |  
     8   itlynn   OP  |  
     10   fy      2016-04-18 01:08:10 +08:00   楼主啊,你以为多重赋值左边的东西真的是元组??  a, b = (a, b) = [a, b] = 1, 2 这三种左侧的字节码是一模一样的,而且跟元组没有任何关系。 你后来 id((a,b)) 这才是建立了一个元组  |  
     11   jiang42      2016-04-18 01:52:38 +08:00 via iPhone    |