I don’t even know how to explain this, so here is the code I’m trying.
from couchdb.schema import Document, TextField
class Base(Document):
type = TextField(default=self.__name__)
#self doesn't work, how do I get a reference to Base?
class User(Base):
pass
#User.type be defined as TextField(default="Test2")
The reason I’m even trying this is I’m working on creating a base class for an orm I’m using. I want to avoid defining the table name for every model I have. Also knowing what the limits of python is will help me avoid wasting time trying impossible things.
The class object does not (yet) exist while the class body is executing, so there is no way for code in the class body to get a reference to it (just as, more generally, there is no way for any code to get a reference to any object that does not exist).
Test2.__name__, however, already does what you’re specifically looking for, so I don’t think you need any workaround (such as metaclasses or class decorators) for your specific use case.Edit: for the edited question, where you don’t just need the name as a string, a class decorator is the simplest way to work around the problem (in Python 2.6 or later):
and put
@maketypein front of each class you want to decorate that way. In Python 2.5 or earlier, you need instead to saymaketype(Base)after each relevantclassstatement.If you want this functionality to get inherited, then you have to define a custom metaclass that performs the same functionality in its
__init__or__new__methods. Personally, I would recommend against defining custom metaclasses unless they’re really indispensable — instead, I’d stick with the simpler decorator approach.