Nested function scope problem

Antoon Pardon apardon at forel.vub.ac.be
Sat Jul 29 12:42:36 EDT 2006


On 2006-07-29, Dennis Lee Bieber <wlfraed at ix.netcom.com> wrote:
> On 28 Jul 2006 17:48:03 GMT, Antoon Pardon <apardon at forel.vub.ac.be>
> declaimed the following in comp.lang.python:
>
>> 
>> That is no reason to say that python has no variables. If someone would
>> explain the difference between objects in some other language and
>> objects in python I wouldn't accept the statement: "python has no
>> objects" either.
>>
> 	Python objects can exist without a "variable" bound to them...
> Though typically such would soon be garbage collected <G>

Well C++ objects can exist without a "variable" bound to them.
We just call them memory leaks <G>

> 	Traditional languages are the other way around... If a variable
> exists, it may exist with no object/value (ie, it is uninitialized -- a
> big problem in C).

What do you call traditional? Lisp is about as old as Fortran AFAIK.

> Python names can not exist (and be used) without
> being bound to some object (even None is a defined object). Attempting
> to use a name that has not been bound gives you the "unbound local" type
> problem.

> 	Yes, some other languages do define special flag values so that they
> can detect the usage of an uninitialized item... But the variable itself
> exists regardless; you can not detach the object from the variable
> (except by assigning something else to the variable).

I'm not so sure Python is that different. The fact that you get
an UnboundLocalError, instead of a NameError, suggests that in
the first case, the 'variable' already exists but is bound to
a "Not yet Bound" value. Not so long ago I was discussing some
implementation details of CPython and someone then said that
all local variables are entered into the local scope at call
time. This was to prevent the language to find variables
that are shadowed on a more global scope because the local
variable wasn't boud yet.

-- 
Antoon Pardon



More information about the Python-list mailing list