[issue11562] += on list inside a tuple raises TypeError but succeds anyway

Daniel Urban report at bugs.python.org
Wed Mar 16 13:15:46 CET 2011


Daniel Urban <urban.dani+py at gmail.com> added the comment:

The reason of this behaviour is that x += 1 basically is the same as x = x.__iadd__(1).  In the tuple case: t[1] = t[1].__iadd__([6]).  The __iadd__ call mutates the list, then the tuple item assignment raises the TypeError. 

See these examples:

>>> import dis
>>> dis.dis('x += 1')
  1           0 LOAD_NAME                0 (x) 
              3 LOAD_CONST               0 (1) 
              6 INPLACE_ADD          
              7 STORE_NAME               0 (x) 
             10 LOAD_CONST               1 (None) 
             13 RETURN_VALUE         
>>> 
>>> dis.dis('t[1] += [6]')
  1           0 LOAD_NAME                0 (t) 
              3 LOAD_CONST               0 (1) 
              6 DUP_TOP_TWO          
              7 BINARY_SUBSCR        
              8 LOAD_CONST               1 (6) 
             11 BUILD_LIST               1 
             14 INPLACE_ADD          
             15 ROT_THREE            
             16 STORE_SUBSCR         
             17 LOAD_CONST               2 (None) 
             20 RETURN_VALUE

----------
nosy: +durban

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue11562>
_______________________________________


More information about the Python-bugs-list mailing list