Python scope is too complicated

Greg Ewing greg at cosc.canterbury.ac.nz
Tue Mar 22 04:04:21 CET 2005


jfj wrote:

> def foo(x):
>      y= (i for i in x)
>      return y
> 
>  From the disassembly it seems that the generator is a code object but
> 'x' is not a cell variable. WTF?

That's because x is not assigned to anywhere in the
body of foo. The bytecode compiler optimizes away the
creation of a cell in this case, just passing the
value of x as an implicit parameter to the generator.

> How do I disassemble the generator?

You'd have to get hold of the code object for it
and disassemble that. There should be a reference to
it in one of the co_consts slots, I think.

-- 
Greg Ewing, Computer Science Dept,
University of Canterbury,	
Christchurch, New Zealand
http://www.cosc.canterbury.ac.nz/~greg



More information about the Python-list mailing list