AFAIK, the class object is unavailable until the class definition is “completed”, so it cannot be obtained during class definition.
If you need a class name for later use, but not to use it during class definition (for example, to compute other field names or some such thing), you can automate the process using the class decorator.
def classname ( field ): def decorator ( klass ): setattr(klass, field, klass.__name__) return klass return decorator
(Caution: not verified.)
With this definition, you can get something like:
@classname(field='x') class Foo: pass
and you get a field x with the class name in it, for example:
print Foo.x
André caron
source share