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:

def methodcaller(name, *args, **kwargs):
    def caller(obj):
        return getattr(obj, name)(*args, **kwargs)
    return caller
doc_python
2016-10-07 17:38:27
Comments
Leave a Comment

Please login to continue.