我試圖創建一個從父類的元類繼承一個類,我不喜歡說:我可以像這樣創建從元類派生的子類嗎?
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給了我下面的消息
class Element(self.Base): NameError: name 'self' is not defined
我怎樣才能做這樣的事情?
預先感謝您。
定義外部類時,類範圍中沒有'self'。無論如何,你爲什麼要這樣做? – Keith 2013-02-14 16:58:32
我想將我所有的數據類放在一個地方 – zorro 2013-02-14 17:26:03
如何使用常規方法並將所有類放入模塊中? – Keith 2013-02-14 17:27:55