Say I want to dynamically create a function on an IPython shell from the following lambda:
f = lambda x, ci: np.percentile(x, 100-ci)
that fixes ci
to a new value. It would be something like the following (create_new_f
is what I am looking for).
ci = 20
new_f = create_new_f(f, ci=ci)
result = new_f([20,30,50,80])
I have tried using functools.partial
as:
new_f = functools.partial(f, ci=20)
but when I run this in an embedded shell in IPython I get:
AttributeError: 'functools.partial' object has no attribute '__module__'
is there any alternative way of doing this, perhaps using decorators?
Background on the topic:
- Here is an IPython issue that prompted the question.
- Here is a thread explaining the problem (and fix in Python 3.x only) as well.
- Here is another thread in SO that shows the problem (see the top comment)
- For an even deeper analysis of this problem, see this issue in IPython.
again, what I am looking for is an alternative way of doing this.