Can I create a subclass derived from metaclass like this?
Question: I tried to create a class that inherit from a metaclass declared in the
superclass, I did like that:

    
    
    from sqlalchemy import Column, String, create_engine
    from sqlalchemy.ext.declarative import declarative_base
    from sqlalchemy.orm import sessionmaker
    
    
    class Database(object):
        """
            Class that holds all data models classes and functions
        """
        def __init__(self, wenDbPath = "test.db"):
            self.engine = create_engine('sqlite:///' + "c:/"+wenDbPath, echo=False)
            self.Session = sessionmaker(bind=self.engine)
            **self.Base** = declarative_base() # This returns a metaclass.
            self.Base.metadata.create_all(self.engine)
            self.session = self.Session()
    
        class Element(**self.Base**):
            __tablename__ = 'elements'
            idNumber = Column(String(255), primary_key=True)
            smallDiscription = Column(String(50), nullable=False)
            longDiscription = Column(String())
            webLink = Column(String())
    
            def __init__(self, idNumber, smallDiscription, longDiscription, webLink):
                self.idNumber = idNumber
                self.longDiscription = longDiscription
                self.smallDiscription = smallDiscription
                self.webLink = webLink
            def __repr__(self):
                return "<Element ('%s : %s')>" % (self.idNumber, self.smallDiscription)
    

Python gives me the following message

> class Element(self.Base): NameError: name 'self' is not defined

How can I do something like this?

Thank you in advance.


Answer: A class body is evaluated before `__init__` runs. Since `Base` doesn't depend
on `__init__` parameters, you can evaluate it at class evaluation time:

    
    
    class Database(object):
        ...
        Base = declarative_base() # This returns a metaclass.
        class Element(Base):
            ...
    

Note that you're using `Base` as a superclass, not a metaclass; metaclass
syntax is `__metaclass__ = Base` or `(metaclass=Base)` depending on version.
See [What is a metaclass in
Python?](http://stackoverflow.com/questions/100003/what-is-a-metaclass-in-
python)

