On 23 Sep 2005 14:01:21 -0700, "Carlos" <[EMAIL PROTECTED]>
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
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._
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
--
http://ma
That doesn't really give him a way of using the class variable inside a method.
Ole
2005/9/24, Benji York <[EMAIL PROTECTED]>:
> 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
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:
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()
--
http://mail.python.org/mailman/listinfo/python-list
You could use self.__class__.X
HTH, Ole
23 Sep 2005 14:01:21 -0700, Carlos <[EMAIL PROTECTED]>:
> 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