我正在用SQLAlchemy创建一个Flask RESTful应用程序。在
我正在努力使用python3中的抽象类概念。根据python文档(https://docs.python.org/3/library/abc.html),用法应该很简单:### Python documentation ###
class Foo:
def __getitem__(self, index):
...
def __len__(self):
...
def get_iterator(self):
return iter(self)
class MyIterable(metaclass=ABCMeta):
@abstractmethod
def __iter__(self):
while False:
yield None
def get_iterator(self):
return self.__iter__()
@classmethod
def __subclasshook__(cls, C):
if cls is MyIterable:
if any("__iter__" in B.__dict__ for B in C.__mro__):
return True
return NotImplemented
MyIterable.register(Foo)
但是,在包中实现时,我收到以下错误:
文件:api.base_api.py
^{pr2}$
文件:api.projects.pyclass ProjectAPI(Resource):
@property
def entity(self):
return Projeto #this is my model class object
def get(self, unique_id=unique_id):
obj self.checkId(unique_id)
return {self.entity.__api_name__: obj.serialize()}
#the return (tested before) is like {"projects": project.data})
...
文件:API.\uu init\uuy.pyfrom apis.base_api import BaseAPI
from apis.projects import ProjectAPI
BaseAPI.register(ProjectAPI)
我没有“编译”错误,应用程序开始运行。当我试图访问ProjectAPI时,我得到了一个错误:AttributeError: 'ProjectAPI' object has no attribute 'checkId'
不是吗BaseAPI.checkid方法被抽象地继承到ProjectAPI,因为我注册了它?我做错什么了??在
提前Tnx
发现了一个错误
当从我的BaseAPI类(换句话说,我的抽象类中的具体类)创建ProjectAPI类时,我应该使用python3语法,扩展抽象类:class ProjectAPI(BaseAPI, Resource):
...
并且不应该在BaseAPI上注册ProjectAPI。在
尽管如此,即使修复此问题,仍会出现以下错误:TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases