Commit b5b37141 authored by Antoine Pitrou's avatar Antoine Pitrou

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

Patch by Brian Thorne.
parent 65a35dca
...@@ -11,7 +11,10 @@ ...@@ -11,7 +11,10 @@
__all__ = ['update_wrapper', 'wraps', 'WRAPPER_ASSIGNMENTS', 'WRAPPER_UPDATES', __all__ = ['update_wrapper', 'wraps', 'WRAPPER_ASSIGNMENTS', 'WRAPPER_UPDATES',
'total_ordering', 'cmp_to_key', 'lru_cache', 'reduce', 'partial'] '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 from collections import namedtuple
try: try:
from _thread import allocate_lock as Lock from _thread import allocate_lock as Lock
...@@ -136,6 +139,29 @@ except ImportError: ...@@ -136,6 +139,29 @@ except ImportError:
pass 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 ### LRU Cache function decorator
################################################################################ ################################################################################
......
This diff is collapsed.
...@@ -1166,6 +1166,7 @@ Tobias Thelen ...@@ -1166,6 +1166,7 @@ Tobias Thelen
Nicolas M. Thiéry Nicolas M. Thiéry
James Thomas James Thomas
Robin Thomas Robin Thomas
Brian Thorne
Stephen Thorne Stephen Thorne
Jeremy Thurgood Jeremy Thurgood
Eric Tiedemann Eric Tiedemann
......
...@@ -124,6 +124,9 @@ Core and Builtins ...@@ -124,6 +124,9 @@ Core and Builtins
Library Library
------- -------
- Issue #12428: Add a pure Python implementation of functools.partial().
Patch by Brian Thorne.
- Issue #16140: The subprocess module no longer double closes its child - Issue #16140: The subprocess module no longer double closes its child
subprocess.PIPE parent file descriptors on child error prior to exec(). subprocess.PIPE parent file descriptors on child error prior to exec().
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment