即使类的所有方法都已实现,我是否可以将其标记为抽象类?

Gon*_*n I 6 python abstract-class abc

有没有办法将 python 类标记为抽象或不可实例化,即使它的所有抽象方法都已实现?

class Component(ABC):
    @abstractmethod
    def operation(self) -> None:
        pass

class Decorator(Component): # I would like this class to be abstract
    def operation(self) -> None:
        print("basic operation")
Run Code Online (Sandbox Code Playgroud)

我发现的唯一解决方法是在类中选择一些方法来同时具有实现和@abstractmethod装饰器,但是随后 python 需要子抽象类的派生者来重新实现该方法。

这也可以通过让子类调用来解决super(),但pylint抱怨这是一个无用的调用。

class Component(ABC):
    @abstractmethod
    def operation(self) -> None:
        pass

class Decorator(Component): # I would like this class to be abstract
    @abstractmethod
    def operation(self) -> None:
        print("basic operation")

class ConcreteDecorator(Decorator): 
    def operation(self) -> None: # python makes child class re-implement
        super().operation()  # pylint complains about useless super delegation
Run Code Online (Sandbox Code Playgroud)

有一个更好的方法吗?
我尝试使用带有实现和装饰器的方法@abstractmethod,但随后派生类需要重新实现。我正在寻找“编译时”的解决方案,而不是运行时错误。

aar*_*ron 1

创建一个辅助类来重写该__new__函数以检查是否cls.__bases__包含自身。

class UnInstantiable:
    def __new__(cls, *args, **kwargs):
        if __class__ in cls.__bases__:
            raise TypeError(f"Can't instantiate un-instantiable class {cls.__name__}")

        return super().__new__(cls)
Run Code Online (Sandbox Code Playgroud)

用法:

# class Decorator(Component):                # Add UnInstantiable base class
class Decorator(Component, UnInstantiable):  # like this
    def operation(self) -> None:
        print("basic operation")


Decorator()  # TypeError: Can't instantiate un-instantiable class Decorator
Run Code Online (Sandbox Code Playgroud)

  • 这是实际的运行时错误还是 IntelliSense 错误? (2认同)