Suppose I have the following function g
:
import numpy as np
from numpy.polynomial import Polynomial as P
def g(x):
return np.log(x)
And I have the following NumPy Polynomial q
:
q = P([0, 1])
I want to put them together like so:
fancy = g / q
# TypeError: unsupported operand type(s) for /: 'Polynomial' and 'function'
So that I can call fancy(x)
, such that it's the equivalent to saying:
g(x) / q(x)
It should be trivial to just say:
def fancy(x):
return g(x) / q(x)
But no, I need the callable function itself, because g
gets redefined at each iteration of a loop.
fancy = lambda x : g(x) / q(x)
Should give you what you want. lambda
expressions are one way python supports creating function object on the fly. I say 'one' because you could def
a function more than once, since it does pretty much the same thing assigning a lambda expression does, defines a function object and assigns it to a reference.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.