是否允许在抽象 class 中使用用户定义的装饰器?或者,它应该在继承之后使用吗?
Is it permitted to use a user defined decorator inside an abstract class? Or, should it be used after inheritance?
例如,假设我有一个我定义的装饰器:decorate
def decorate(func):
def inside_func(*args, **kwargs):
# Do something
return func(*args, **kwargs)
return inside_func
接下来,假设我正在写一个名为 Model
的摘要 class
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
或者,应该是:
from abc import ABC, abstractmethod
class Model(ABC):
def __init__(self, value):
self.value = value
super().__init__()
@abstractmethod
def do_something(self):
pass
# Inherit the base class
class DoAdd42(Model):
@decorate # <----------------------- SHOULD @decorate BE HERE INSTEAD?
def do_something(self):
return self.value + 42
如果两者都允许,是否有 "best-practices" 方法来做到这一点?
两者都允许。因为abstractmethod
,你的decorate
和class里面的something
都是函数。您可以将它们打印到您的控制台以进行验证。
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
的理解,做出自己的判断。
例如,假设我有一个我定义的装饰器:decorate
def decorate(func):
def inside_func(*args, **kwargs):
# Do something
return func(*args, **kwargs)
return inside_func
接下来,假设我正在写一个名为 Model
的摘要 classfrom 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
或者,应该是:
from abc import ABC, abstractmethod
class Model(ABC):
def __init__(self, value):
self.value = value
super().__init__()
@abstractmethod
def do_something(self):
pass
# Inherit the base class
class DoAdd42(Model):
@decorate # <----------------------- SHOULD @decorate BE HERE INSTEAD?
def do_something(self):
return self.value + 42
如果两者都允许,是否有 "best-practices" 方法来做到这一点?
两者都允许。因为abstractmethod
,你的decorate
和class里面的something
都是函数。您可以将它们打印到您的控制台以进行验证。
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
的理解,做出自己的判断。