1 回答

TA贡献1735条经验 获得超5个赞
两者都允许。因为abstractmethod, yourdecorate和somethingin class 都是函数。您可以将它们打印到控制台以进行验证。
In [2]: def decorate(func):
...: def inside_func(*args, **kwargs):
...: # Do something
...: return func(*args, **kwargs)
...: return inside_func
...:
In [7]: from abc import ABC, abstractmethod
...:
...: class Model(ABC):
...: def __init__(self, value):
...: self.value = value
...: super().__init__()
...:
...: @abstractmethod
...: @decorate # <-------------------- IS @decorate ALLOWED HERE?
...: def do_something(self):
...: pass
...: # even no self is valid for class in Python3+
...: def whatever():
...: print('fff')
...:
In [8]: print(abstractmethod)
<function abstractmethod at 0x100ff86a8>
In [9]: print(Model.do_something)
<function decorate.<locals>.inside_func at 0x1041031e0>
In [10]: print(Model.whatever)
<function Model.whatever at 0x103b48950>
In [11]: Model.whatever()
fff
我不熟悉abstractmethod,所以我不知道最佳实践和有关它的规则的更多细节。你可以试着根据我给你的信息,结合你自己对abstractmethod.
添加回答
举报