summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorAntony Lee <anntzer.lee@gmail.com>2021-09-24 15:22:49 (GMT)
committerGitHub <noreply@github.com>2021-09-24 15:22:49 (GMT)
commit6587fc60d447603fb8c631d81d9bb379f53c39ab (patch)
treec52c3172f8845e2cb51eed4338436c9e6f58965a /Lib
parent8d8729146f21f61af66e70d3ae9501ea6bdccd09 (diff)
downloadcpython-6587fc60d447603fb8c631d81d9bb379f53c39ab.zip
cpython-6587fc60d447603fb8c631d81d9bb379f53c39ab.tar.gz
cpython-6587fc60d447603fb8c631d81d9bb379f53c39ab.tar.bz2
bpo-44019: Implement operator.call(). (GH-27888)
Having `operator.call(obj, arg)` mean `type(obj).__call__(obj, arg)` is consistent with the other dunder operators. The semantics with `*args, **kwargs` then follow naturally from the single-arg semantics.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/operator.py7
-rw-r--r--Lib/test/test_operator.py12
2 files changed, 19 insertions, 0 deletions
diff --git a/Lib/operator.py b/Lib/operator.py
index 241fdbb..72105be 100644
--- a/Lib/operator.py
+++ b/Lib/operator.py
@@ -221,6 +221,12 @@ def length_hint(obj, default=0):
raise ValueError(msg)
return val
+# Other Operations ************************************************************#
+
+def call(obj, /, *args, **kwargs):
+ """Same as obj(*args, **kwargs)."""
+ return obj(*args, **kwargs)
+
# Generalized Lookup Objects **************************************************#
class attrgetter:
@@ -423,6 +429,7 @@ __not__ = not_
__abs__ = abs
__add__ = add
__and__ = and_
+__call__ = call
__floordiv__ = floordiv
__index__ = index
__inv__ = inv
diff --git a/Lib/test/test_operator.py b/Lib/test/test_operator.py
index b9b8f15..cf3439f 100644
--- a/Lib/test/test_operator.py
+++ b/Lib/test/test_operator.py
@@ -518,6 +518,18 @@ class OperatorTestCase:
with self.assertRaises(LookupError):
operator.length_hint(X(LookupError))
+ def test_call(self):
+ operator = self.module
+
+ def func(*args, **kwargs): return args, kwargs
+
+ self.assertEqual(operator.call(func), ((), {}))
+ self.assertEqual(operator.call(func, 0, 1), ((0, 1), {}))
+ self.assertEqual(operator.call(func, a=2, obj=3),
+ ((), {"a": 2, "obj": 3}))
+ self.assertEqual(operator.call(func, 0, 1, a=2, obj=3),
+ ((0, 1), {"a": 2, "obj": 3}))
+
def test_dunder_is_original(self):
operator = self.module