That was my point - I consider python's ordinary use of lexical scoping
to be a good thing, and I was wondering why this "good thing" was not
used in classes, as well as outside of classes.
But it is. You're mistaking lexical scoping for object attribute access.
The rules for lexical scoping inside a class are (almost) the same as
they are for inside a function:
def parrot(breed):
def message(colour):
return "The %s %s has beautiful plumage." % (breed, colour)
return message("Blue")
class Parrot:
def parrot(self, breed):
def message(colour):
return "The %s %s has beautiful plumage." % (breed, colour)
return message("Blue")
And in use:
'The Swedish Blue has beautiful plumage.'
Notice that to have lexical scoping work, you actually have to nest the
functions. Otherwise they are in different scopes.
This might lead you believe you can do this:
class Parrot2:
colour = "Blue"
def parrot(self, breed):
return "The %s %s has beautiful plumage." % (breed, colour)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 4, in parrot
NameError: global name 'colour' is not defined
What's going on? Why doesn't the parrot method see the name "colour" in
the class scope?
The reason is that the class scope is deliberately left out of the nested
scope chain. This was a design decision from when nested scopes were
introduced:
"An alternative would have been to allow name binding in class
scope to behave exactly like name binding in function scope. This
rule would allow class attributes to be referenced either via
attribute reference or simple name. This option was ruled out
because it would have been inconsistent with all other forms of
class and instance attribute access, which always use attribute
references. Code that used simple names would have been obscure."
http://www.python.org/dev/peps/pep-0227/
So inside the method, you need to refer to Parrot.colour (or thanks to
the rules of attribute inheritance, self.colour).
Classes could be closures, but that could radically change the way
methods and classes work. Depending on design decisions, it might require
huge changes to the Python compiler. How would it change the existing
lexical scoping in factory functions?
def factory(colour):
class Parrot:
def parrot(self, breed):
return "The %s %s has beautiful plumage." % (breed, colour)
return Parrot
'The Swedish Red has beautiful plumage.'
Consider a hypothetical Python with classes included in the lexical
scoping:
class Parrot3:
colour = "Blue"
def parrot(self):
colour = "Red"
What should method parrot do? I can think of at least three possibilities:
* create a local name colour inside the method scope;
* change the class attribute Parrot3.colour to "Red";
* create an instance attribute self.colour.
Whatever solution you come up with, there is potential inconsistency with
other parts of the language.