472,364 Members | 2,058 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,364 software developers and data experts.

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 1311
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: John Collyer | last post by:
Hi, In assembly language you can use a lookup table to call functions. 1. Lookup function address in table 2. Call the function Like: CALL FUNCTION
10
by: philchen1978 | last post by:
Hi, I can compile the code below with GCC 3.4.2, because function g is a "dependent name". template<class T> void f1(T t) { g(t); }
1
by: Srini | last post by:
I was reading the "Exceptional C++" of Herb Sutter. In an example, he mentions the following. // In some library header: namespace N { class C{}; } int operator+(int i, N::C) { return i+1; }...
29
by: Stefan Slapeta | last post by:
There have been many announciations that VC 8.0 will finally support two phase template lookup; now, after I installing VC 2005 Express I had to realize that it still doesn't! What has happend...
17
by: sounak | last post by:
How could we get a macro name from a macro value such that in a header file #define a 100 #define b 200 now the source file will be such that the user gives 100 then the value is outputted as...
70
by: jojoba | last post by:
Hello! Does anyone know how to find the name of a python data type. Conside a dictionary: Banana = {} Then, how do i ask python for a string representing the name of the above dictionary...
2
by: Rolf | last post by:
Hello- I’m integrating blogging software (www.b2evolution.net) into my app and have run into a namespace-collision problem. In my code, I’ve defined a User class, and so have the b2evolution...
4
by: StephQ | last post by:
I need to know if it is possible to solve the following problem (I am really stuck at it). Consider the CRTP pattern: struct Derived { ....// May contain void operate( double x ) , may not....
2
by: Stefan Naewe | last post by:
Hi there. Given the following code, why is class B::A used at point //2 and not class ::A ? Does inheriting a class in a namespace put the names of that namespace into the "name pool" used for...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.