Python 2.7 Combine abc.abstractmethod and classmethod

前端 未结 4 1438
太阳男子
太阳男子 2020-12-15 04:19

How do I create a decorator for an abstract class method in Python 2.7?

Yes, this is similar to this question, except I would like to combine abc.abstractmet

4条回答
  •  南方客
    南方客 (楼主)
    2020-12-15 04:55

    You could upgrade to Python 3.

    Starting with Python 3.3, it is possible to combine @classmethod and @abstractmethod:

    import abc
    class Foo(abc.ABC):
        @classmethod
        @abc.abstractmethod
        def my_abstract_classmethod(...):
            pass
    

    Thanks to @gerrit for pointing this out to me.

提交回复
热议问题