Issue #12428: Add a pure Python implementation of functools.partial().

Patch by Brian Thorne.
This commit is contained in:
Antoine Pitrou 2012-11-13 21:35:40 +01:00
parent 65a35dcadd
commit b5b3714168
4 changed files with 167 additions and 73 deletions

View file

@ -11,7 +11,10 @@
__all__ = ['update_wrapper', 'wraps', 'WRAPPER_ASSIGNMENTS', 'WRAPPER_UPDATES',
'total_ordering', 'cmp_to_key', 'lru_cache', 'reduce', 'partial']
from _functools import partial, reduce
try:
from _functools import reduce
except ImportError:
pass
from collections import namedtuple
try:
from _thread import allocate_lock as Lock
@ -136,6 +139,29 @@ except ImportError:
pass
################################################################################
### partial() argument application
################################################################################
def partial(func, *args, **keywords):
"""new function with partial application of the given arguments
and 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
try:
from _functools import partial
except ImportError:
pass
################################################################################
### LRU Cache function decorator
################################################################################