nested scopes

Jeremy Hylton jeremy at
Fri Feb 9 18:55:12 CET 2001

In article <96059d$din$1 at>,
  Joshua Marshall <jmarshal at> wrote:

> > But they *do* complain about having to make explicit
> > references to 'self', which is the analogous thing there.
> Still this is a different issue than lexical scoping.  Consider:
>    class T:
>       def m(self):
>          print self.x
>    T.x = 'foo'
>    T().m()
> Here, T().m() prints "foo".  Lexically, x is not defined in m's scope.
> I believe it was a good decision to require going through self to
> access class/object fields.  This is very different than something
> like:
>    def f():
>       x = 1
>       def g():
>          print x
>       g()
> where x is lexically in g's scope.

I agree that these examples are very different.  In the first, the
variable name is self and x is an attribute.  In the second, x is itself
a variable.  Python has very different rules for name lookup and
attribute lookup.

The few complaints I have seen about nested scopes on this list all
describe dynamic behavior that is probably better done with objects and
attributes.  While nested scopes are useful, many of the uses people put
them to in other languages are probably best done with instances in

-- Jeremy Hylton, <>

Sent via

More information about the Python-list mailing list