scope of function parameters

Ian Kelly ian.g.kelly at gmail.com
Sun May 29 15:12:16 EDT 2011


On Sun, May 29, 2011 at 12:38 PM, Chris Angelico <rosuav at gmail.com> wrote:
> I thought it basically functioned top-down. You get a different error
> on the print line if there's a "bar = 42" *after* it. This could make
> debugging quite confusing.
>
> Guess it's just one of the consequences of eschewing variable
> declarations. Sure it's easier, but there's complications down the
> road.

It's also a consequence of local variable access being optimized with
different bytecode: the type of storage has to be determined at
compile time.  The compiler could in principle figure out that "bar"
cannot be bound at that point and make it a global reference, but it
is easy to concoct situations involving loops or conditionals where
the storage cannot be determined at compile time, and so the compiler
follows the simple rule of making everything local unless it's never
assigned.

Cheers,
Ian



More information about the Python-list mailing list