2.1 nested scope SyntaxWarning

John J. Lee jjl at pobox.com
Sat Nov 3 15:44:34 EST 2001


Upgrading to Python 2.1, I find my old 2.0 code works fine, but I'm getting
warnings like the following:

/usr/local/lib/python2.1/site-packages/ls_fit/utils/graph.py:355:
SyntaxWarning: local name 'grace_np' in 'define_grace_np' shadows use of
'grace_np' as global in nested scope 'GracePlotter'
  def define_grace_np():
/usr/local/lib/python2.1/site-packages/ls_fit/utils/graph.py:668:
SyntaxWarning: local name 'qt' in 'QwtPlotter' shadows use of 'qt' as global in
nested scope 'hardcopy'
  class QwtPlotter(BasePlotter):


This is with code like the following (no from __future__ import x statements):

[...]
def define_grace_np():
    import grace_np
    class GracePlotter(BasePlotter):
[...]
        def _ensure_init(self):
[...]
            if self._initialised == 0:
                self._g = grace_np.GraceProcess()
[...]
    return GracePlotter

Now that I come to think about it, I'm not entirely sure what's going on when
you run this in 2.0, or in 2.1 without from __future__ import nested_scopes.  I
don't see why grace_np should be visible as a local variable in the
GracePlotter class, given that the following:

def foo()
    a = 1
    def bar():
        print a
    bar()

foo()

causes a NameError on 'print a', as does this:

def foo():
    a = 1
    class bar:
        def baz(s):
        print a
    return bar

bar = foo()
b = bar()
b.baz()

However, the GracePlotter class works fine.  Why?

Thanks for any help.


John




More information about the Python-list mailing list