带有abc的Python3抽象类多重继承引发错误

小提琴

嗨,我想知道是否可以继承一个类和metaclass = ABCMeta

我尝试了如下代码。但是,它会引发错误。

SyntaxError:位置参数紧跟关键字参数

这是我的课。基类将具有一些共享功能(带有实现)和类变量。然后,UserBase将没有任何实现。最后,办公室用户将继承UserBase。

它可能会像这样。

Base -> UserBase -> OfficeUser
Base -> UserBase -> OnSiteUser
Base -> UsUserBase -> OnSiteUser
Base -> UsUserBase -> OnSiteUser




class Base():
    def __init__(self):
        print('test')

    def shared_function_with_implementation():
        print('shared function')

# This class will not have any implementation
class UserBase(metaclass=ABCMeta, Base):

    def __init__(self):
        print('test')
        super().__init__()

    @abstractmethod
    def print_name():
        pass

class OfficeUser(UserBase):
    def __init__(self):
        print('OfficeUser')
        super().__init__()

    def print_name():
        # implementation
迪米特里斯·法萨拉基斯·希利亚德(Dimitris Fasarakis Hilliard)

类定义语句如下来自于如何将参数传递函数调用已知的规则:

classdef    ::=  [decorators] "class" classname [inheritance] ":" suite
inheritance ::=  "(" [argument_list] ")"
classname   ::=  identifier

其中argument_list在定义上的调用的段

您需要在位置参数之后提供任何关键字参数

在这种情况下,metaclass应该在基类arg之后提供kwarg:

class UserBase(Base, metaclass=ABCMeta)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章