Logo

Programming-Idioms

History of Idiom 37 > diff from v21 to v22

Edit summary for version 22 by 222:
[Python] 2222

Version 21

2017-02-07, 22:40:35

Version 22

2017-03-20, 08:02:01

Idiom #37 Currying

Technique of transforming a function that takes multiple arguments and returning a function for which some of the arguments are preset.

Idiom #37 Currying

Technique of transforming a function that takes multiple arguments and returning a function for which some of the arguments are preset.

Imports
from functools import partial
Imports
from functools import partial
Code
def f(a, b):
	return a+b

add_to_two = partial(f, 2)
Code
def f(a):
	def add(b):
		return a+b
	return add

print (f(2)(1)

#add_to_two = partial(f, 2)
Doc URL
https://docs.python.org/3/library/functools.html#functools.partial
Doc URL
https://docs.python.org/3/library/functools.html#functools.partial