【发布时间】:2017-08-11 03:45:18
【问题描述】:
当使用classmethod动态改变子类中的方法时,如何动态改变方法的签名?
示例
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) 一起使用。这个方法不是直接的。
【问题讨论】:
标签: python class methods signature