mirror of
https://github.com/python/cpython.git
synced 2025-10-10 00:43:41 +00:00
Issue #12428: Add a pure Python implementation of functools.partial().
Patch by Brian Thorne.
This commit is contained in:
parent
65a35dcadd
commit
b5b3714168
4 changed files with 167 additions and 73 deletions
|
@ -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
|
||||
################################################################################
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue