global variable not seen (bug?)

Peter Hansen peter at engcorp.com
Thu Jan 9 09:02:11 EST 2003


Dennis Lee Bieber wrote:
> 
> Peter Hansen fed this fish to the penguins on Wednesday 08 January 2003
> 06:49 pm:
> 
> >>>>> a = 5
> >>>> def p1():
> > ...   b = 6
> > ...   print a, b
> > ...   def p2():
> > ...     c = 7
> > ...     print a, b, c
> > ...     def p3():
> > ...        d = 8
> > ...        print a, b, c, d
> > ...     p3()
> > ...   p2()
> > ...
> >>>> p1()
> > 5 6
> > 5 6 7
> > 5 6 7 8
> >>>>
> >
>         Okay, unmasqueraded /read/ access is available (and possibly -- if the
> item is mutable, mutations. But raw /write/ access is not.

Of course!  Without declaring variables before their use, how could
you expect the compiler to know in which scope to place the newly
created name.  It does the only thing it can: place it in the local
namespace if it is a local, or the global one if defined as a global.
Anything else would seem unexpected.  This is Python, after all, and
not Pascal.

Out of curiousity though: what means can one use to access the other
scopes, if the need arises (which, one would hope, it never does)?

-Peter




More information about the Python-list mailing list