Return a callable object that calls the given method on its operand. After f = methodcaller('name'), the call f(r) returns r.name(). After g = methodcaller('name', 'date', foo=1), the call g(r) returns r.name('date', foo=1).
| 308 | return self.__class__, self._items |
| 309 | |
| 310 | class methodcaller: |
| 311 | """ |
| 312 | Return a callable object that calls the given method on its operand. |
| 313 | After f = methodcaller('name'), the call f(r) returns r.name(). |
| 314 | After g = methodcaller('name', 'date', foo=1), the call g(r) returns |
| 315 | r.name('date', foo=1). |
| 316 | """ |
| 317 | __slots__ = ('_name', '_args', '_kwargs') |
| 318 | |
| 319 | def __init__(self, name, /, *args, **kwargs): |
| 320 | self._name = name |
| 321 | if not isinstance(self._name, str): |
| 322 | raise TypeError('method name must be a string') |
| 323 | self._args = args |
| 324 | self._kwargs = kwargs |
| 325 | |
| 326 | def __call__(self, obj, /): |
| 327 | return getattr(obj, self._name)(*self._args, **self._kwargs) |
| 328 | |
| 329 | def __repr__(self): |
| 330 | args = [repr(self._name)] |
| 331 | args.extend(map(repr, self._args)) |
| 332 | args.extend('%s=%r' % (k, v) for k, v in self._kwargs.items()) |
| 333 | return '%s.%s(%s)' % (self.__class__.__module__, |
| 334 | self.__class__.__name__, |
| 335 | ', '.join(args)) |
| 336 | |
| 337 | def __reduce__(self): |
| 338 | if not self._kwargs: |
| 339 | return self.__class__, (self._name,) + self._args |
| 340 | else: |
| 341 | from functools import partial |
| 342 | return partial(self.__class__, self._name, **self._kwargs), self._args |
| 343 | |
| 344 | |
| 345 | # In-place Operations *********************************************************# |
no outgoing calls
searching dependent graphs…