Need help with Python scoping rules
kj
no.email at please.post
Thu Aug 27 19:49:27 EDT 2009
Miles Kaufmann <milesck at umich.edu> 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:
>http://mail.python.org/pipermail/python-dev/2000-November/010598.html
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.foo()
>1
>1
> >>> o.x = 2
> >>> o.foo()
>1
>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!
kynn
More information about the Python-list
mailing list