Python - 函数继承 - 更改关键字参数

Python - Functional inheritance - changing keyword arguments

是否可以使用某些关键字参数定义函数,然后引用同一函数,但使用不同的关键字参数值。

例如我有以下

def f_beta(x,a=2.7,b=3.05):
    """The un-normalised beta distribution function."""
    return math.pow(x, a - 1.0)*math.pow(1.0 - x, b - 1.0)

我想做的事情相当于:

f = f_beta
g = f_beta(a=1.0, b=10.0)

其中 f 是初始函数的副本,g 是相同的函数,但关键字参数的默认值不同。有没有一种方法可以为 g 执行此操作,而无需将 f_beta 构建到 class 中,也无需重新编写各种函数。

动机: 我创建了一个 class,它有一个成员函数 init_dist,在某些时候我在 __init__() 中通过以下方式定义它:

self.init_dist = f_beta

并希望能够传入一个新函数。

我查看了以下内容:

并没有找到任何答案,或者我应该在哪里寻找参考资料。

使用functools.partial:

Python 2.7.6 (default, Jun 22 2015, 17:58:13) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from functools import partial
>>> import math
>>> def f_beta(x,a=2.7,b=3.05):
...     """The un-normalised beta distribution function."""
...     return math.pow(x, a - 1.0)*math.pow(1.0 - x, b - 1.0)
... 
>>> f = f_beta
>>> g = partial(f_beta, a=1.0, b=10.0)
>>> f()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: f_beta() takes at least 1 argument (0 given)
>>> g()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: f_beta() takes at least 1 argument (2 given)
>>> g(13)
-5159780352.0
>>> f(13, 1.0, 10.0)
-5159780352.0
>>>