nested scopes

Joshua Marshall jmarshal at mathworks.com
Fri Feb 9 02:12:45 EST 2001


Greg Ewing <greg at cosc.canterbury.ac.nz> wrote:
> Tim Peters wrote:
>> 
>> It's curious how
>> few have ever complained that a def inside a class doesn't have automagic
>> access to the class locals.

> 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.



More information about the Python-list mailing list