469,955 Members | 2,494 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,955 developers. It's quick & easy.

Name lookup inside class definition

Hello. Consider the following two examples:
class Test1(object):
att1 = 1
def func(self):
print Test1.att1 // ok

class Test2(object):
att1 = 1
att2 = Test2.att1 // NameError: Name Test2 is not defined

It seems a little strange. Why a class name can be used in a method
while cannot be used in the class block itself? I read the "Python
Reference Manual"(4.1 Naming and binding ), but didn't get a clue.
Jun 27 '08 #1
3 1253
On Tue, 17 Jun 2008 23:05:56 -0700, WaterWalk wrote:
Hello. Consider the following two examples: class Test1(object):
att1 = 1
def func(self):
print Test1.att1 // ok

class Test2(object):
att1 = 1
att2 = Test2.att1 // NameError: Name Test2 is not defined

It seems a little strange. Why a class name can be used in a method
while cannot be used in the class block itself? I read the "Python
Reference Manual"(4.1 Naming and binding ), but didn't get a clue.
It's because functions actually defer the name lookup. So you can use
*any* name in a function, basically. If it's there at the function's
runtime (not its declaration time), you're okay.

During the execution of a class body, the class is not yet created. So
you're running this ``Test2.att1`` lookup already (it has to be executed
*now*, during the class creation) and fail because the class is not there.

You can still refer to the class' scope as a local scope::
>>class A(object):
... att1 = 1
... att2 = att1 + 2
...
>>A.att1
1
>>A.att2
3

HTH,

--
Robert "Stargaming" Lehmann
Jun 27 '08 #2
WaterWalk a écrit :
Hello. Consider the following two examples:
class Test1(object):
att1 = 1
def func(self):
print Test1.att1 // ok
or
print type(self).att1

class Test2(object):
att1 = 1
att2 = Test2.att1 // NameError: Name Test2 is not defined

It seems a little strange. Why a class name can be used in a method
while cannot be used in the class block itself?
class is an executable statement. The whole "class" block is first
eval'd, then the class object is created and bound to it's name.

So when the function is called, the class statement has already been
executed, the class object created and bound to the name Test1. But when
the att2=Test2.att1 is executed, the class object doesn't yet exists,
nor the name Test2.

Anyway, you don't need to refer to the class name here:

class Toto(object):
titi = 1
toto = titi + 1
Jun 27 '08 #3
Ah, I see. Thank you all.
Jun 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by John Collyer | last post: by
10 posts views Thread by philchen1978 | last post: by
1 post views Thread by Srini | last post: by
29 posts views Thread by Stefan Slapeta | last post: by
17 posts views Thread by sounak | last post: by
70 posts views Thread by jojoba | last post: by
4 posts views Thread by StephQ | last post: by
2 posts views Thread by Stefan Naewe | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.