Skip to content

Commit d95b3c2

Browse files
committed
Add opeator.call, such that operator.call(obj, *args, **kwargs) == obj(*args, **kwargs)
1 parent f51b130 commit d95b3c2

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

Lib/operator.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,12 @@ def length_hint(obj, default=0):
221221
raise ValueError(msg)
222222
return val
223223

224+
# Other Operations ************************************************************#
225+
226+
def call(obj, /, *args, **kwargs):
227+
"""Same as obj(*args, **kwargs)."""
228+
return obj(*args, **kwargs)
229+
224230
# Generalized Lookup Objects **************************************************#
225231

226232
class attrgetter:
@@ -423,6 +429,7 @@ def ixor(a, b):
423429
__abs__ = abs
424430
__add__ = add
425431
__and__ = and_
432+
__call__ = call
426433
__floordiv__ = floordiv
427434
__index__ = index
428435
__inv__ = inv

Lib/test/test_operator.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,18 @@ def __length_hint__(self):
518518
with self.assertRaises(LookupError):
519519
operator.length_hint(X(LookupError))
520520

521+
def test_call(self):
522+
operator = self.module
523+
524+
def func(*args, **kwargs): return args, kwargs
525+
526+
self.assertEqual(operator.call(func), ((), {}))
527+
self.assertEqual(operator.call(func, 0, 1), ((0, 1), {}))
528+
self.assertEqual(operator.call(func, a=2, obj=3),
529+
((), {"a": 2, "obj": 3}))
530+
self.assertEqual(operator.call(func, 0, 1, a=2, obj=3),
531+
((0, 1), {"a": 2, "obj": 3}))
532+
521533
def test_dunder_is_original(self):
522534
operator = self.module
523535

0 commit comments

Comments
 (0)