Need help with Python scoping rules

kj at
Fri Aug 28 01:49:27 CEST 2009

Miles Kaufmann <milesck at> writes:

>On Aug 26, 2009, at 1:11 PM, kj wrote:
>> I think I understand the answers well enough.  What I *really*
>> don't understand is why this particular "feature" of Python (i.e.
>> that functions defined within a class statement are forbidden from
>> "seeing" other identifiers defined within the class statement) is
>> generally considered to be perfectly OK.  IMO it's a bizarre,
>> inexplicable blindspot (which, among other things, gives rise to
>> a certain worry about what other similar craziness lurks under
>> Python's image of rationality).  I have never seen even a half-hearted
>> justification, from a language design point of view, for why this
>> particular "feature" is worth having.

>Guido's design justifications:

Ah!  Clarity!  Thanks!  How did you find this?  Did you know of
this post already?  Or is there some special way to search Guido's
"design justifications"?

>...because the suite  
>namespace and the class namespace would get out of sync when different  
>objects were assigned to the class namespace:

>class C:
>   x = 1
>   def foo(self):
>       print x
>       print self.x

> >>> o = C()
> >>>
> >>> o.x = 2
> >>>

But this unfortunate situation is already possible, because one
can already define

class C:
   x = 1
   def foo(self):
       print C.x
       print self.x

which would lead to exactly the same thing.

I need to learn more about metaclasses, though, to fully understand
your post.

Many thanks!


More information about the Python-list mailing list