python - Most pythonic way to write high order function -
it should basic question wonder what's pythonic way handle high order function. have f
, g
defined:
def f(x): homecoming x**2 def g(x): homecoming x**3 def gen_func(f,g): def func(x): homecoming f(x)+g(x) homecoming func wanted_func = gen_func(f, g)
or:
import functools def gen_func(f,g,x): homecoming f(x)+g(x) wanted_func = functools.partial(gen_func, f, g)
and there may point miss these 2 writing differ?
well, assuming don't utilize gen_func
elsewhere, eliminate in favour of next one-liner:
wanted_func = functools.partial(lambda f, g, x: f(x) + g(x), f, g)
...but isn't readable, , partial function application redundant. might write:
wanted_func = lambda x: f(x) + g(x)
but, examples, there no differences (that know of) between them, it's you. eschew "pythonic" dogma , utilize whatever consider readable , whatever appropriate particular utilize case, not python community considers correct.
remember python language and, spoken languages, prescriptive rules how they're used tend limit expressiveness. compare newspeak.
python functools
No comments:
Post a Comment