Skip to main content

Benx Shen's Library tagged code   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
20 Jun 07

Bertrand's weblog: When was this Java class compiled?

  • When was this Java class compiled?



    Credits to Dmitry Beransky on the advanced-java@discuss.develop.com list.
    import java.util.Date;
    import java.io.IOException;
    public class When {
    public static void main(String args[]) throws IOException {

    Date d =
    new Date(
    When.class.getResource("When.class")
    .openConnection()
    .getLastModified()
    );

    System.out.println("This class was compiled on " + d);
    }
    }
1 - 20 of 20
Showing 20 items per page

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

Join Diigo