Is it me or a python wart

Robin Becker robin at jessikat.fsnet.co.uk
Sat Apr 27 13:28:58 EDT 2002


In article <ucjicvlrf7djd3 at news.supernews.com>, John Roth
<johnroth at ameritech.net> writes
>
>"Just van Rossum" <just at xs4all.nl> wrote in message
>news:just-9E8624.12281026042002 at news1.xs4all.nl...
>> In article <C2cBaXANXSy8Ewbb at jessikat.demon.co.uk>,
......
>
>This doesn't appear to be the same bug. Robin's example
>has two functions which are identical, other than the
>substitution of 'x' for 'z' in the definition of class A.
>
>The discussion in the bug report indicates that the 'x'
>following the '=' should be compiled using the 'LOAD_NAME'
>op code, which implements the old rules. If it in fact did this,
>I would expect the first example to return 4, not 2,
>because 'LOAD_NAME' is supposed to ignore the
>parameter.
>
>Clearly, it isn't. At a guess, I'd think the problem is that
>the compiler is trying to make 'z' a local variable of the
>def, so it's using a different op code in the first case than
>in the second.
>

well I see this from dis the x=x case seems to ignore that line totally,
the z=x case uses LOAD_CLOSURE. I guess someone is guessing that x=x is
a NOOP and removing it as an optimisation, but perhaps I'm totally
confused as well. Probably I'm misunderstanding the scope somehow, but
in this case  x = x is supposed to be equivalent to A.x = x. 

if it's treated as A.x = A.x then I should be warned that A.x is
referred to before assignment. 

>>> dis.dis(aaa.bingo)
          0 SET_LINENO               2

          3 SET_LINENO               3
          6 LOAD_CONST               1 ('A')
          9 BUILD_TUPLE              0
         12 LOAD_CLOSURE             0 (x)
         15 LOAD_CONST               2 (<code object A at 010EE328, file
"aaa.py", line 3>)
         18 MAKE_CLOSURE             0
         21 CALL_FUNCTION            0
         24 BUILD_CLASS         
         25 STORE_FAST               1 (A)

         28 SET_LINENO               7
         31 LOAD_FAST                1 (A)
         34 CALL_FUNCTION            0
         37 LOAD_ATTR                1 (get)
         40 CALL_FUNCTION            0
         43 RETURN_VALUE        
         44 LOAD_CONST               0 (None)
         47 RETURN_VALUE        
>>> dis.dis(aaa.bango)
          0 SET_LINENO              10

          3 SET_LINENO              11
          6 LOAD_CONST               1 ('A')
          9 BUILD_TUPLE              0
         12 LOAD_CONST               2 (<code object A at 010F2398, file
"aaa.py", line 11>)
         15 MAKE_FUNCTION            0
         18 CALL_FUNCTION            0
         21 BUILD_CLASS         
         22 STORE_FAST               1 (A)

         25 SET_LINENO              15
         28 LOAD_FAST                1 (A)
         31 CALL_FUNCTION            0
         34 LOAD_ATTR                1 (get)
         37 CALL_FUNCTION            0
         40 RETURN_VALUE        
         41 LOAD_CONST               0 (None)
         44 RETURN_VALUE        
>>> 
>Of course, I could be totally confused, as usual.
>
>John Roth
>
>

-- 
Robin Becker



More information about the Python-list mailing list