Python doesn't follow it's own scoping rules?
Fredrik Lundh
fredrik at pythonware.com
Wed Dec 1 18:46:52 EST 1999
Tim Danner <tdanner at drpepper.baker.rice.edu> wrote:
> Consider this python program:
>
> a = 0
>
> def death_and_destruction():
> a = a + 1
>
> death_and_destruction()
> print a
>
> My understanding is that it should print "1".
that's only because you haven't read the
FAQ or the language reference. read on.
> Unfortunately, it prints:
>
> Traceback (innermost last):
> File "evil.py", line 6, in ?
> death_and_destruction()
> File "evil.py", line 4, in death_and_destruction
> a = a + 1
> NameError: a
>
> This is very strange, and in general Not Good.
checking the FAQ before positing is Good,
though:
http://www.python.org/doc/FAQ.html#4.57
(summary: if Python has determined that a name
is local, it doesn't look it up in the global name-
space).
in addition to the FAQ, I suggest reading the
language reference, especially the section
titled "code blocks, execution frames, and
namespaces".
</F>
More information about the Python-list
mailing list