Python 如何用不同的方法参数名称定义两个相同的类?
我正在开发一个科学图书馆,在那里我将定义由FFT连接的时域和频域中的向量函数。我为频域中的向量公式创建了一个类,现在我想为时域定义一个相同的类 我希望在时域中,类函数-尽管与频域孪生函数相同-有一个名为t而不是omega的参数。在保持可读性的同时,是否有更简单的方法来实现这一点,而不是重复定义每个方法 我的代码: 注意:我的类要复杂得多,不能只将函数用作formula.x_func…-包括一些检查等。另外,实际上有6个组件Python 如何用不同的方法参数名称定义两个相同的类?,python,python-3.x,class,abc,Python,Python 3.x,Class,Abc,我正在开发一个科学图书馆,在那里我将定义由FFT连接的时域和频域中的向量函数。我为频域中的向量公式创建了一个类,现在我想为时域定义一个相同的类 我希望在时域中,类函数-尽管与频域孪生函数相同-有一个名为t而不是omega的参数。在保持可读性的同时,是否有更简单的方法来实现这一点,而不是重复定义每个方法 我的代码: 注意:我的类要复杂得多,不能只将函数用作formula.x_func…-包括一些检查等。另外,实际上有6个组件 听起来您可以通过优雅地使用类继承来实现所需 请检查或。在创建类后,向类添
听起来您可以通过优雅地使用类继承来实现所需
请检查或。在创建类后,向类添加方法很容易,它们只是类属性。这里最困难的部分是,您需要动态创建新函数,从原始类克隆方法,以便能够更改其签名 这不是Python最清晰的部分,官方参考文档中没有记录动态函数创建,但可以在下面的文档中找到: 所以这里有一个可能的方法:
# have the new class derive from the common base
class TimeFormula(VecFormula):
"same as FreqFormula, but the omega parameter is renamed to t"
pass
# loop over methods of origina class
for i,j in inspect.getmembers(FreqFormula, inspect.isfunction):
# copy the __init__ special method
if i == '__init__':
setattr(TimeFormula, i, j)
elif i.startswith('__'): continue # ignore all other special attributes
if not j.__qualname__.endswith('.'.join((FreqFormula.__name__, i))):
continue # ignore methods defined in parent classes
# clone the method from the original class
spec = inspect.getfullargspec(j)
newspec = inspect.FullArgSpec(['t' if i == 'omega' else i
for i in spec.args], *spec[1:])
f = types.FunctionType(j.__code__, j.__globals__, i, newspec, j.__closure__)
f.__qualname__ = '.'.join((TimeFormula.__qualname__, i))
# adjust the signature
sig = inspect.signature(j)
if ('omega' in sig.parameters):
f.__signature__ = sig.replace(
parameters = [p.replace(name='t') if name == 'omega' else p
for name, p in sig.parameters.items()])
# and finally insert the new method in the class
setattr(TimeFormula, i, f)
从我的示例和文档的这一部分可以看出,我知道类继承。我看不出这能解决我的问题。请你在回答中包括相关部分,并帮助我理解这些知识如何实现我的目标?另外,另一个链接没有相关信息,它只是一个通用类继承教程,显示了我在代码中已经使用的方法。感谢您的时间,但就目前情况而言,这个答案很遗憾没有帮助。为什么不在VecFormula中添加常用函数或属性,然后通过继承VecFormula创建两个新类。@JasonYang代码将用于科学环境,因此函数定义将使用正确的参数名,而不是func.xx,y,z之类的名称,params更可取。是的,我觉得它看起来不会那么漂亮。。。我不会用代码来代替重新定义7个方法,但看看如何做到这一点很有趣!
# have the new class derive from the common base
class TimeFormula(VecFormula):
"same as FreqFormula, but the omega parameter is renamed to t"
pass
# loop over methods of origina class
for i,j in inspect.getmembers(FreqFormula, inspect.isfunction):
# copy the __init__ special method
if i == '__init__':
setattr(TimeFormula, i, j)
elif i.startswith('__'): continue # ignore all other special attributes
if not j.__qualname__.endswith('.'.join((FreqFormula.__name__, i))):
continue # ignore methods defined in parent classes
# clone the method from the original class
spec = inspect.getfullargspec(j)
newspec = inspect.FullArgSpec(['t' if i == 'omega' else i
for i in spec.args], *spec[1:])
f = types.FunctionType(j.__code__, j.__globals__, i, newspec, j.__closure__)
f.__qualname__ = '.'.join((TimeFormula.__qualname__, i))
# adjust the signature
sig = inspect.signature(j)
if ('omega' in sig.parameters):
f.__signature__ = sig.replace(
parameters = [p.replace(name='t') if name == 'omega' else p
for name, p in sig.parameters.items()])
# and finally insert the new method in the class
setattr(TimeFormula, i, f)