[Python] `staticmethod` and `abc.abstractmethod`: Will it blend?

Discussão em 'Python' iniciado por Stack, Setembro 7, 2025.

  1. Stack

    Stack Membro Participativo

    In my Python app I want to make a method that is both a staticmethod and an abc.abstractmethod. How do I do this?

    I tried applying both decorators, but it doesn't work. If I do this:

    import abc

    class C(object):
    __metaclass__ = abc.ABCMeta

    @abc.abstractmethod
    @staticmethod
    def my_function(): pass


    I get an exception*, and if I do this:

    class C(object):
    __metaclass__ = abc.ABCMeta

    @staticmethod
    @abc.abstractmethod
    def my_function(): pass


    The abstract method is not enforced.

    How can I make an abstract static method?

    *The exception:

    File "c:\Python26\Lib\abc.py", line 29, in abstractmethod
    funcobj.__isabstractmethod__ = True
    AttributeError: 'staticmethod' object has no attribute '__isabstractmethod__'

    Continue reading...

Compartilhe esta Página