[Python-Dev] LOAD_NAME & classes

Guido van Rossum guido@python.org
Mon, 22 Apr 2002 16:11:40 -0400


[Guido]
> > would work:
> >
> >     x = 10
> >     def f():
> >         print x
> >         x = 12
> >     f()

[Tim]
> But this still wouldn't:
> 
>     x = 10
>     def f():
>         print x
> 
>     def g():
>         x = 12
>         f()
> 
>     g()
> 
> You can't explain why that doesn't print 12 without insisting that
> different textual regions have different scopes.  Newbie complaints
> and misunderstandings follow.  Under an "only runtime" conception,
> the only obvious behavior is that x is bound to 12 at the time f()
> is called, so of course it should print 12.  It takes a certain
> sophistication to understand that the name "x" means different
> things in different parts of the file.  Once that's understood, the
> behavior of your example is no longer a mystery; but before it's
> understood, your example is only one of countless confusions.

I think this is a red herring, and not an argument against what I
proposed.  The "only runtime" rules doesn't require dynamic scopes (I
agree that dynamic scopes would be bad).  Dynamic scopes, and your
example, mix up the call context with the definition context.  My
example takes the definition context, and applies the "is x defined in
this scope?" test at runtime instead of at compile time.  Very
different!

--Guido van Rossum (home page: http://www.python.org/~guido/)