The functools package in Python has a function called partial which is similar to, but not the same as Currying.
It helps us create a new function where some of the original arguments have fixed values.
It can be done manually:
examples/python/generate_partials.py
from functools import partial
def full(number, name):
print(f"{name} {number}")
foo = partial(full, name='FOO')
foo(17)
but sometimes you'd like to do that on-the fly with a number of different values of the same parameter.
It can be done using exec:
examples/python/generate_partials_on_the_fly_with_exec.py
from functools import partial
def full(number, name):
print(f"{name} {number}")
names = ["foo"]
for name in names:
exec(f'{name} = partial(full, name="{name.upper()}")')
foo(17)
but there is a better way to do it using locals.
manually:
examples/python/generate_partials_using_locals.py
from functools import partial
def full(number, name):
print(f"{name} {number}")
locals()["foo"] = partial(full, name='Foo')
foo(17)
on-the fly to many values:
examples/python/generate_partials_on_the_fly_with_locals.py
from functools import partial
def full(number, name):
print(f"{name} {number}")
names = ["foo"]
for name in names:
locals()[name] = partial(full, name=name.upper())
foo(17)