473,402 Members | 2,053 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Accessing class variable at class creation time

Hi!

class A:
X = 2
def F():
print A.X
F()

The above fails because the name A is not
yet at global scope when the reference A.X
is reached. Is there any way to refer to A.X
without making explicit use of the name 'A'?
Admittedly the code looks pretty unusual,
but I'm defining configuration "minilanguages"
to configure different parts of an application,
each inside a configuration class (like A),
and I find this very convenient except for
the above problem.

Thank you in advance
Regards,
Carlos

Sep 23 '05 #1
7 1066
You could use self.__class__.X

HTH, Ole
23 Sep 2005 14:01:21 -0700, Carlos <ca************@gmail.com>:
Hi!

class A:
X = 2
def F():
print A.X
F()

The above fails because the name A is not
yet at global scope when the reference A.X
is reached. Is there any way to refer to A.X
without making explicit use of the name 'A'?
Admittedly the code looks pretty unusual,
but I'm defining configuration "minilanguages"
to configure different parts of an application,
each inside a configuration class (like A),
and I find this very convenient except for
the above problem.

Thank you in advance
Regards,
Carlos

--
http://mail.python.org/mailman/listinfo/python-list

Sep 23 '05 #2
It might be that I'm complicating something easy here, but I
immediately thought of

import sys

class A:
X = 2
def F():
f = sys._getframe().f_back
print f.f_locals["X"]
F()

Sep 23 '05 #3
Carlos wrote:
Hi!

class A:
X = 2
def F():
print A.X
F()

The above fails because the name A is not
yet at global scope when the reference A.X
is reached. Is there any way to refer to A.X
without making explicit use of the name 'A'?


How about this:
class A:

.... X = 2
.... print X
....
2
--
Benji York

Sep 24 '05 #4
That doesn't really give him a way of using the class variable inside a method.

Ole
2005/9/24, Benji York <be***@benjiyork.com>:
Carlos wrote:
Hi!

class A:
X = 2
def F():
print A.X
F()

The above fails because the name A is not
yet at global scope when the reference A.X
is reached. Is there any way to refer to A.X
without making explicit use of the name 'A'?


How about this:
>>> class A:

... X = 2
... print X
...
2
--
Benji York

--
http://mail.python.org/mailman/listinfo/python-list

Sep 24 '05 #5
Jan-Ole Esleben wrote:
That doesn't really give him a way of using the class variable inside a method.


Oh! I must have misunderstood the question. I'd like to know more
about why the OP wants to do this; a small example would help narrow
down the possibilities.
--
Benji York
Sep 24 '05 #6
Thank you all!

After all, I found at least three more or less convenient alternatives:

1) Pass X as default parameter to F.
2) Set globals() from inside A, something like globals()['A_locals'] =
locals() or globals()['A_X'] = X. Then access A_locals or A_X from F.
3) Use sys._getframe(1) or sys._getframe().f_back to get the caller
frame.

But the following won't work:
self.__class__.X
Because there is no self around here, I'm not trying to access X from a
class instance but instead from code running at class definition time.
class A:
... X = 2
... print X


Of course this will print X but the point was to do it from inside a
function.

Regards,
Carlos

Sep 25 '05 #7
On 23 Sep 2005 14:01:21 -0700, "Carlos" <ca************@gmail.com>
wrote:
Hi!

class A:
X = 2
def F():
print A.X
F()

The above fails because the name A is not
yet at global scope when the reference A.X


Maybe I'm missing something. Python 2.4.1#65 under Win32 Idle 1.1.1
gives me the following:

--begin included file---
class A:

X = 2
def F():
print A.X
F()
2
---end included file---

But what good is F? You can't call A.F() because methods need an
instance, and calling A.F(instance) or instance.F() throws a TypeError
(argument count) exception.

Regards,

-=Dave
--
Change is inevitable, progress is not.
Sep 26 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

106
by: A | last post by:
Hi, I have always been taught to use an inialization list for initialising data members of a class. I realize that initialsizing primitives and pointers use an inialization list is exactly the...
5
by: Sandeep | last post by:
Hi, In the following code, I wonder how a private member of the class is being accessed. The code compiles well in Visual Studio 6.0. class Sample { private: int x; public:
7
by: Klaus Johannes Rusch | last post by:
Is the following code valid and supported by current implementations? function somename() { this.show = function () { document.write("somename called") } } var somename = new somename();...
5
by: David W. Fenton | last post by:
Why is it that this never actually works: Set clsQuery = Nothing What actually happens is that the class module gets re-initialized. Is this because my clsQuery variable is declared as New?...
9
by: Bob Day | last post by:
VS 2003, vb.net , sql msde... I have an application with multiple threads running. Its a telephony application where each thread represents a telephone line. For code that would be the same...
37
by: Joergen Bech | last post by:
(Slightly religious question): Suppose I have the following class: ---snip--- Public Class MyClass Private _MyVariable As Integer Public Property MyVariable() As Integer Get
5
by: Andy | last post by:
I'm having trouble accessing an unmanaged long from a managed class in VC++.NET When I do, the contents of the variable seem to be mangled. If I access the same variable byte-by-byte, I get the...
2
by: Jurek Dabrowski | last post by:
hi all, I have a question in reference to accessing variables in another class maybe someone has dealt with before. I have some public variables declared in my main plug-in class...
12
by: titan nyquist | last post by:
I have a class with data and methods that use it. Everything is contained perfectly THE PROBLEM: A separate thread has to call a method in the current instantiation of this class. There is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
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...

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.