python中abcmeta_在Python3中不能使用抽象类(ABCMeta)

在使用Python3的抽象类ABCMeta时遇到问题。尝试在BaseAPI(抽象类)中注册ProjectAPI(子类)导致AttributeError。解决方案是使用Python3的语法直接扩展抽象类,但这样又会引发TypeError关于元类冲突的问题。
摘要由CSDN通过智能技术生成

我正在用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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值