Unfortunately, in Python, a class attribute cannot reference its class name. The following raises a NameError
:
class Foo(object):
bar = Foo
In a situation where a class attribute must reference its class name (in my case, it's part of a validation scheme for a library). What is the clearest way to do so?
The following is my current attempt:
class Foo(object):
bar = None
Foo.bar = Foo
This works, but even with comments is likely to cause confusion, since you expect a class attribute to be initialized when declared, not after the class is declared.
Does anyone have a better workaround?