Skip to main content

Diigo Home

9.8. functools - Higher order functions and operations on callable objects - P... - The Diigo Meta page

docs.python.org/...functools.html - Cached - Annotated View

Benx Shen's personal annotations on this page

benxshen
Benxshen bookmarked on 2009-10-20 python code
  • 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'

This link has been bookmarked by 1 people . It was first bookmarked on 20 Oct 2009, by Benx Shen.

  • 20 Oct 09
    • 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'