operator.methodcaller()

operator.methodcaller(name[, args...])

Return a callable object that calls the method name on its operand. If additional arguments and/or keyword arguments are given, they will be given to the method as well. For example:

  • After f = methodcaller('name'), the call f(b) returns b.name().
  • After f = methodcaller('name', 'foo', bar=1), the call f(b) returns b.name('foo', bar=1).

Equivalent to:

1
2
3
4
def methodcaller(name, *args, **kwargs):
    def caller(obj):
        return getattr(obj, name)(*args, **kwargs)
    return caller
doc_python
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.