| title | Python create partial functions in runtime | |||
|---|---|---|---|---|
| timestamp | 2021-09-29 01:30:01 -0700 | |||
| tags |
|
|||
| published | true | |||
| books | python |
|||
| author | szabgab | |||
| archive | true | |||
| show_related | true |
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:
{% include file="examples/python/generate_partials.py" %}
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:
{% include file="examples/python/generate_partials_on_the_fly_with_exec.py" %}
but there is a better way to do it using locals.
manually:
{% include file="examples/python/generate_partials_using_locals.py" %}
on-the fly to many values:
{% include file="examples/python/generate_partials_on_the_fly_with_locals.py" %}