functools

New In Python 2.5

partial

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.

The partial() is used for partial function application which “freezes” some portion of a function’s arguments and/or keywords resulting in a new object with a simplified signature. For example, partial() can be used to create a callable that behaves like the int() function where the base argument defaults to two:

>>> from functools import partial
>>> basetwo = partial(int, base=2)
>>> basetwo.__doc__ = 'Convert base 2 string to an int.'
>>> basetwo('10010')
18

update_wrapper

wraps

total_ordering

New In Python 2.7

Given a class defining one or more rich comparison ordering methods, this class decorator supplies the rest. This simplifies the effort involved in specifying all of the possible rich comparison operations:

The class must define one of __lt__(), __le__(), __gt__(), or __ge__(). In addition, the class should supply an __eq__() method.

In [70]: @total_ordering
   ....: class Student:
   ....:         def __eq__(self, other):
   ....:                 return ((self.lastname.lower(), self.firstname.lower()) ==
   ....:                         (other.lastname.lower(), other.firstname.lower()))
   ....:         def __lt__(self, other):
   ....:                 return ((self.lastname.lower(), self.firstname.lower()) <
   ....:                         (other.lastname.lower(), other.firstname.lower()))
   ....:

In [71]: print dir(Student)
['__doc__', '__eq__', '__ge__', '__gt__', '__le__', '__lt__', '__module__']

References

[1] Docs@Python, 9.8. functools — Higher-order functions and operations on callable objects

results matching ""

    No results matching ""