scope of generators, class variables, resulting in global na

dontspamleo dontsendleospam at
Sun Feb 28 00:57:15 CET 2010

I think a big part of the problem is that the scoping rules in Python
are inconsistent because classes are a different kind of object. An
example helps:

This works:

x = 1
def f(y): return y + x

This works:

def f():
  x = 1
  def g(y): return x + y
  return g(2)

But this doesn't work...
class C:
  x = 1
  def f(self,y): return x + y

...although what was probably meant was this, which does work...
class C:
  x = 1
  def f(self,y): return self.x + y

...and really means this...
class C:
  x = 1
  def f(self,y): return T.x + y

...which create other quirkiness when x is mutable as illustrated
nicely here:

One reasonable answer to this quirkiness is RTFM. Classes are well
documented as is everything else in python. Mostly late-binding
semantics are also well documented.

I argue that it is more consistent to have the scope for classes be
consistent with the scope of everything else, which makes the early/
late binding point mute.

I know this is a major change, that it would break existing code, etc.
It would have been better to talk about these things before py3k.

1. Has this been discussed before?

1. What would this suggestion break?

2. What are the advantages of making the scope of class variables
different? Maybe is it just a historical trait?


More information about the Python-list mailing list