[Python] How do I list the inherited abstractmethods of an abstract subclass in Python?

Discussão em 'Python' iniciado por Stack, Julho 6, 2025.

  1. Stack

    Stack Membro Participativo

    How can I see the attributes inherited by an abstract class subclassing from another?

    For example:

    class MammalAbstract(ABC):

    @abstractmethod
    def legs(self):
    pass

    class CanineAbstract(MammalAbstract):

    @abstractmethod
    def tail(self):
    pass

    class PekineseAbstract(CanineAbstract):

    @abstractmethod
    def yip(self):
    pass


    So Canine instances must have legs and tail, and Pekinese additionally yip.

    Is there any way to get the PekineseAbstract and see what attributes it must expose, including those it inherits? Or is the only thing to do for readability to repeat the abstractmethods inherited?

    As I understand it, one point of abstract classes is to clarify code by providing a contract that can be inspected to see what attributes a class must expose. But if these code blocks are scattered over several files, a reader has to find and read all those files to know what a Canine or a Pekinese must support.

    I looked at inspect but it doesn't have anything handy, though maybe has the tools to build something that does this.

    Continue reading...

Compartilhe esta Página