[Python-Dev] Visibility scope for "for/while/if" statements

Greg Ewing greg.ewing at canterbury.ac.nz
Sat Sep 24 05:02:03 CEST 2005

Nick Coghlan wrote:

> Python, however, uses a dynamic name binding system and scopes are expensive 
> because they require setting up all of the machinery to support nested 
> visibility.

Scopes within a function needn't be anywhere near as expensive
as scopes for nested functions are. The compiler can just
allocate all the locals that are going to be needed on
entry to the function, and keep track of which ones are

The only runtime overhead would be unbinding locals as they
go out of scope, if that were considered necessary or desirable.

The main problem is Python's lack of declarations, which
means that nested locals could only be used in places
where it's clear that a new variable is being introduced,
such as for loops, LCs, etc.


More information about the Python-Dev mailing list