Skip to content

Instantly share code, notes, and snippets.

@bruno-uy
Created May 27, 2021 19:42
Show Gist options
  • Save bruno-uy/ca4212d48c9824bcad6d432cfbaff5ee to your computer and use it in GitHub Desktop.
Save bruno-uy/ca4212d48c9824bcad6d432cfbaff5ee to your computer and use it in GitHub Desktop.
Usage and update of arguments in partial function
from functools import partial
def print_n_dict_elem(n, dictionary, elem):
print(n * dictionary[elem])
# Create a new function that print elem 3 times
print_3_dict_elem = partial(print_n_dict_elem, n=3, dictionary={"a": "A", "b": "B"})
print_3_dict_elem(elem="a")
# Update dictionary passed as an argument
print_3_dict_elem.keywords["dictionary"].update({"c": "C"})
print_3_dict_elem(elem="c")
@bruno-uy
Copy link
Author

Thanks @kchuang-homelight for the inspiration 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment