partial
-
demo
>>> from functools import partial >>> basetwo = partial(int, base=2) >>> basetwo.__doc__ = 'convert base 2 string to an int' >>> basetwo('10010') 18
demo
-
make new version of a function with one or more arguments already filled in
-
new version of a function documents itself
-
demo exponentiation
-
power
def power(base, exponent): return base ** exponent
-
square
def square(base): return power(base, 2)
-
cube
def cube(base): return power(base, 3)
-
use partial
from functools import partial square = partial(power, exponent=2) cube = partial(power, exponent=3) def test_partial(): assert square(2) == 4 assert cube(2) == 8 def test_partial_docs(): assert square.keywords == {'exponent': 2} assert square.func == power def test_power_partials(): power_partials = [] for x in range(1, 11): f = partial(power, exponent=x) power_partials.append(f) # use list comprehension instead of loop [partial(power, exponent=x) for x in range(1, 11)] assert power_partials[0][2] == 2 assert power_partials[4][2] == 32 assert power_partials[9][2] == 1024
-