1
當使用類方法來動態改變子類中的方法時,如何動態改變方法的簽名?如何動態更改子類中方法的簽名?
例如
import inspect
class ModelBase(object):
@classmethod
def method_one(cls, *args):
raise NotImplementedError
@classmethod
def method_two(cls, *args):
return cls.method_one(*args) + 1
class SubClass(ModelBase):
@staticmethod
def method_one(a, b):
return a + b
test = SubClass()
try:
print(inspect.signature(test.method_two))
except AttributeError:
print(inspect.getargspec(test.method_two).args)
我想test.method_two
得到的test.method_one
簽名。如何重寫父類ModelBase
?我已閱讀Preserving signatures of decorated functions。在python3.4 +中,functools.wraps
有助於保留裝飾函數的簽名。我想將它應用於類方法。
當使用functools.wraps
時,我需要指定裝飾方法的名稱。但在這種情況下如何訪問classmethod
以外的裝飾方法?
from functools import wraps
class ModelBase(object):
@classmethod
def method_one(cls, *args):
raise NotImplementedError
@classmethod
def method_two(cls):
@wraps(cls.method_one)
def fun(*args):
return cls.method_one(*args) + 1
return fun
method_two
返回一個包裝的函數,但我必須與test.method_two()(*arg)
使用它。這種方法不是直接的。
謝謝!這是CodeGen的。要爲method_two執行codegen,它需要method_one的簽名。 – Ryan