New in version 2.5.
The functional module is for higher-order functions: functions that act on or return other functions. In general, any callable object can be treated as a function for the purposes of this module.
The functional module defines the following function:
func[,*args][, **keywords]) |
def partial(func, *args, **keywords): def newfunc(*fargs, **fkeywords): newkeywords = keywords.copy() newkeywords.update(fkeywords) return func(*(args + fargs), **newkeywords) newfunc.func = func newfunc.args = args newfunc.keywords = keywords return newfunc
The partial is used for partial function application which ``freezes'' some portion of a function's arguments and/or keywords resulting in a new object with a simplified signature. For example, partial can be used to create a callable that behaves like the int function where the base argument defaults to two:
>>> basetwo = partial(int, base=2) >>> basetwo.__doc__('Convert base 2 string to an int.') >>> basetwo('10010') 18