Nested scopes hitch
Graham Ashton
gashton at cmedltd.com
Mon Apr 8 06:25:43 EDT 2002
On Fri, 2002-04-05 at 20:49, Jeff Shannon wrote:
> In class A, the attribute a is a class attribute, not a name in an
> enclosing scope, so the nested scope rules don't apply. You can
> access it by referring to self.a, however -- that will find both
> instance and class attributes.
I remember being slightly surprised by the fact that self finds class
variables. Is there any particular reason why, considering that the you
can reference them as ClassName.var anyway?
I suspect that my surpise stemmed form an assumption that readibility
would be improved if you can tell just by looking at it whether a
variable is a class or instance variable.
I'm just curious...
Python 2.2 (#2, Mar 11 2002, 13:24:00)
[GCC 2.95.3 20010315 (release)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> class Spam:
... a = 1
... def test(self):
... if Spam.a == self.a:
... print "equal"
... if Spam.a is self.a:
... print "identical"
...
>>> s = Spam()
>>> s.test()
equal
identical
--
Graham Ashton
More information about the Python-list
mailing list