Accessing class variable at class creation time

Benji York benji at benjiyork.com
Fri Sep 23 21:38:36 EDT 2005


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




More information about the Python-list mailing list