getting a reference to a class inside its definition

dan dan at eevolved.com
Mon Jun 18 10:37:52 EDT 2001


I think I could communicate my question better with code ;)

class SomeClass:
    clsref = SomeClass # raises NameError
    clsref = self             # also raises NameError

I understand why those two don't work. The first one because SomeClass 
isn't added to the locals namespace until the interpreter is finished 
reading the class definition. The second one doesn't work because 'self' 
isn't a special keyword, it's just a convention that everybody uses it as 
the first arg in a class method (and even then, many don't).

Still, I want to be able to reference a class inside it's own definition. 
Is this possible or am I going to have to do

class SomeClass: 
    pass
SomeClass.clsref = SomeClass

because that's what I'm doing now...

Thank you!
Dan



More information about the Python-list mailing list