473,788 Members | 2,854 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1368
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
19034
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
1836
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
1996
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; } // A mainline to exercise it: #include <numeric> int main() {
29
2110
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 to these plans??? Stefan
17
7095
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 a the user gives 200 then the value is outputted as b
70
27514
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 (i.e. 'Banana')?
2
2792
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 writers, so there’s a collision. I tried simply renaming their User class to something else throughout their code (e.g., BlogUser) but as a first pass it didn’t work since the “User” name is hardcoded in many places in their code where the class is...
4
1619
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
1533
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 unqualified lookup? Where do I find that in the "Holy Standard" ? Thanks, Stefan
0
9656
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10364
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10172
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9967
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7517
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5536
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4069
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.