Accessing class variable at class creation time

Carlos carlosjosepita at gmail.com
Sun Sep 25 16:55:09 EDT 2005


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




More information about the Python-list mailing list