summaryrefslogtreecommitdiffstats
path: root/Lib/functools.py
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2012-11-13 20:35:40 (GMT)
committerAntoine Pitrou <solipsis@pitrou.net>2012-11-13 20:35:40 (GMT)
commitb5b371416843b2b12ef2010fa1b9b257327ac91a (patch)
tree6288b9b518ae5c564e0754e3cbed3b84ec32c87e /Lib/functools.py
parent65a35dcadd668b122d6235e9b9bd43380c1a098b (diff)
downloadcpython-b5b371416843b2b12ef2010fa1b9b257327ac91a.zip
cpython-b5b371416843b2b12ef2010fa1b9b257327ac91a.tar.gz
cpython-b5b371416843b2b12ef2010fa1b9b257327ac91a.tar.bz2
Issue #12428: Add a pure Python implementation of functools.partial().
Patch by Brian Thorne.
Diffstat (limited to 'Lib/functools.py')
-rw-r--r--Lib/functools.py28
1 files changed, 27 insertions, 1 deletions
diff --git a/Lib/functools.py b/Lib/functools.py
index 226a46e..60d2cf1 100644
--- a/Lib/functools.py
+++ b/Lib/functools.py
@@ -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
@@ -137,6 +140,29 @@ except ImportError:
################################################################################
+### 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
################################################################################