访问子类中覆盖方法的签名
Accessing signatures of overriding methods in subclasses
在 python3.8.5 中,我正在使用一个现有的 class superCl
,它定义了一个方法 f
,以及多个现有的子 classes sub1
、sub2
... 用不同数量的变量覆盖 f
。基于,我想通过superclass向所有子class添加一个新函数g
。新函数应该调用 f
并用它做一些事情。问题是每个子 class 对 f
都有不同的签名。我怎样才能解决这个问题?示例代码如下:
import numpy as np
class superCl(object): # Exists already
def f(self, x): return x
class sub1(superCl): # Exists already
def f(self, x, a, b): return x / (b - a)
class sub2(superCl): # Exists already
def f(self, x, tau): return 1/tau * np.exp(-x/tau)
sc = superCl()
print(sc.f(0))
s1 = sub1()
print(s1.f(0, 1, 2))
s2 = sub2()
print(s2.f(0, 1))
def g(self, x1, x2, ...): # New, calls existing method. Is there some way to get the signatures and values from the subclasses, eg self.args?
print('Extra meth')
self.f(x1, ...) - self.f(x2, ...) # How to adapt to proper arguments for f for each sublcass?
superCl.g = g # New method gets added to new and existing instances
print(sc.g(0, 1))
print(s1.g(0, 1, 1, 2))
print(s2.g(0, 1, 1))
编辑:Superclass 定义了一组通用函数; subclasses 是特例,每个都有不同的变量。 Function f
returns subclasses 参数的函数值;函数 g
我正在尝试创建评估 f
在两个不同点的差异。
看起来像使用 *args
或 **kwds
可以 (Positional argument v.s. keyword argument):
import numpy as np
class superCl(object): # Exists already
def f(self, x): return x
class sub1(superCl): # Exists already
def f(self, x, a, b): return x / (b - a)
class sub2(superCl): # Exists already
def f(self, x, tau): return 1/tau * np.exp(-x/tau)
sc = superCl()
print(sc.f(0))
s1 = sub1()
print(s1.f(0, 1, 2))
s2 = sub2()
print(s2.f(0, 1))
print('===========================================')
def g(self, x1, x2, *args):
print("args", *args)
return self.f(x2, *args) - self.f(x1, *args)
superCl.g = g
print("--------------------\n"); print("sc", sc.f(1), '-', sc.f(0), "=", sc.g(0, 1))
print("--------------------\n"); print("s1", s1.f(1, 0, 4), '-', s1.f(0, 0, 4), "=", s1.g(0, 1, 0, 4))
print("--------------------\n"); print("s2", s2.f(1, 5), '-', s2.f(0, 5), "=", s2.g(0, 1, 5))
print('===========================================')
def h(self, x1, x2, **args):
print("args", args)
return self.f(x2, **args) - self.f(x1, **args)
superCl.h = h
print("--------------------\n"); print("sc", sc.f(1), '-', sc.f(0), "=", sc.h(0, 1))
print("--------------------\n"); print("s1", s1.h(0, 1, a=0, b=4))
print("--------------------\n"); print("s2", s2.h(0, 1, tau=5))
在 python3.8.5 中,我正在使用一个现有的 class superCl
,它定义了一个方法 f
,以及多个现有的子 classes sub1
、sub2
... 用不同数量的变量覆盖 f
。基于g
。新函数应该调用 f
并用它做一些事情。问题是每个子 class 对 f
都有不同的签名。我怎样才能解决这个问题?示例代码如下:
import numpy as np
class superCl(object): # Exists already
def f(self, x): return x
class sub1(superCl): # Exists already
def f(self, x, a, b): return x / (b - a)
class sub2(superCl): # Exists already
def f(self, x, tau): return 1/tau * np.exp(-x/tau)
sc = superCl()
print(sc.f(0))
s1 = sub1()
print(s1.f(0, 1, 2))
s2 = sub2()
print(s2.f(0, 1))
def g(self, x1, x2, ...): # New, calls existing method. Is there some way to get the signatures and values from the subclasses, eg self.args?
print('Extra meth')
self.f(x1, ...) - self.f(x2, ...) # How to adapt to proper arguments for f for each sublcass?
superCl.g = g # New method gets added to new and existing instances
print(sc.g(0, 1))
print(s1.g(0, 1, 1, 2))
print(s2.g(0, 1, 1))
编辑:Superclass 定义了一组通用函数; subclasses 是特例,每个都有不同的变量。 Function f
returns subclasses 参数的函数值;函数 g
我正在尝试创建评估 f
在两个不同点的差异。
看起来像使用 *args
或 **kwds
可以 (Positional argument v.s. keyword argument):
import numpy as np
class superCl(object): # Exists already
def f(self, x): return x
class sub1(superCl): # Exists already
def f(self, x, a, b): return x / (b - a)
class sub2(superCl): # Exists already
def f(self, x, tau): return 1/tau * np.exp(-x/tau)
sc = superCl()
print(sc.f(0))
s1 = sub1()
print(s1.f(0, 1, 2))
s2 = sub2()
print(s2.f(0, 1))
print('===========================================')
def g(self, x1, x2, *args):
print("args", *args)
return self.f(x2, *args) - self.f(x1, *args)
superCl.g = g
print("--------------------\n"); print("sc", sc.f(1), '-', sc.f(0), "=", sc.g(0, 1))
print("--------------------\n"); print("s1", s1.f(1, 0, 4), '-', s1.f(0, 0, 4), "=", s1.g(0, 1, 0, 4))
print("--------------------\n"); print("s2", s2.f(1, 5), '-', s2.f(0, 5), "=", s2.g(0, 1, 5))
print('===========================================')
def h(self, x1, x2, **args):
print("args", args)
return self.f(x2, **args) - self.f(x1, **args)
superCl.h = h
print("--------------------\n"); print("sc", sc.f(1), '-', sc.f(0), "=", sc.h(0, 1))
print("--------------------\n"); print("s1", s1.h(0, 1, a=0, b=4))
print("--------------------\n"); print("s2", s2.h(0, 1, tau=5))