Skip to main content

Benx Shen's Library tagged python   View Popular

9.8. functools — Higher order functions and operations on callable objects — Python v3.1.1 documentation

  • functools.partial(func, *args, **keywords)

    Return a new partial object which when called will behave like func
    called with the positional arguments args and keyword arguments keywords. If
    more arguments are supplied to the call, they are appended to args. If
    additional keyword arguments are supplied, they extend and override keywords.
    Roughly equivalent to:


    def partial(func, *args, **keywords):
    def newfunc(*fargs, **fkeywords):
    newkeywords = keywords.copy()
    newkeywords.update(fkeywords)
    return func(*(args + fargs), **newkeywords)
    newfunc.func = func
    newfunc.args = args
    newfunc.keywords = keywords
    return newfunc
  • functools.wraps(wrapped, assigned=WRAPPER_ASSIGNMENTS, updated=WRAPPER_UPDATES)

    This is a convenience function for invoking partial(update_wrapper,
    wrapped=wrapped, assigned=assigned, updated=updated)
    as a function decorator
    when defining a wrapper function. For example:


    >>> from functools import wraps
    >>> def my_decorator(f):
    ... @wraps(f)
    ... def wrapper(*args, **kwds):
    ... print('Calling decorated function')
    ... return f(*args, **kwds)
    ... return wrapper
    ...
    >>> @my_decorator
    ... def example():
    ... """Docstring"""
    ... print('Called example function')
    ...
    >>> example()
    Calling decorated function
    Called example function
    >>> example.__name__
    'example'
    >>> example.__doc__
    'Docstring'

10.9. operator — Standard operators as functions — Python v2.7a0 documentation

  • operator.itemgetter(item[, args...])

    Return a callable object that fetches item from its operand using the
    operand’s __getitem__() method. If multiple items are specified,
    returns a tuple of lookup values. Equivalent to:


    def itemgetter(*items):
    if len(items) == 1:
    item = items[0]
    def g(obj):
    return obj[item]
    else:
    def g(obj):
    return tuple(obj[item] for item in items)
    return g

Caller and Callee « ActiveState Code

  • Recipe 576925: Caller and Callee











    A recipe to find out which function is the caller of the current function.












    Python


    1
    2
    3
    4
    5
    6
    7
    import inspect

    def callee():
    return inspect.getouterframes(inspect.currentframe())[1][1:4]

    def caller():
    return inspect.getouterframes(inspect.currentframe())[2][1:4]
1 - 20 of 29 Next ›
Showing 20 items per page

Highlighter, Sticky notes, Tagging, Groups and Network: integrated suite dramatically boosting research productivity. Learn more »

Join Diigo