Accessing class variable at class creation time

C

Carlos

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
 
S

Simon Percivall

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()
 
B

Benji York

Carlos said:
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:
.... X = 2
.... print X
....
2
 
J

Jan-Ole Esleben

That doesn't really give him a way of using the class variable inside a method.

Ole
 
B

Benji York

Jan-Ole Esleben said:
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.
 
C

Carlos

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
 
D

Dave Hansen

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--- 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top