Python3抽象类多继承与abc抛出错误

时间:2017-02-06 22:53:08

标签: python python-3.x

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

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

  

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

这是我的课程。基类将具有一些共享函数(带有实现)和类变量。然后,UserBase将没有任何实现。最后,office用户将继承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

1 个答案:

答案 0 :(得分:3)

class definition statement遵循函数调用中有关参数传递方式的已知规则:

argument_list

the section on Calls中定义了metaclass

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

在这种情况下,应在基类arg之后提供class UserBase(Base, metaclass=ABCMeta) kwarg:

request