Changeset 391 for python/trunk/Doc/library/functools.rst
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Doc/library/functools.rst
r2 r391 1 :mod:`functools` --- Higher 1 :mod:`functools` --- Higher-order functions and operations on callable objects 2 2 ============================================================================== 3 3 4 4 .. module:: functools 5 :synopsis: Higher 5 :synopsis: Higher-order functions and operations on callable objects. 6 6 .. moduleauthor:: Peter Harris <scav@blueyonder.co.uk> 7 7 .. moduleauthor:: Raymond Hettinger <python@rcn.com> … … 9 9 .. sectionauthor:: Peter Harris <scav@blueyonder.co.uk> 10 10 11 .. versionadded:: 2.5 11 12 12 .. versionadded:: 2.5 13 **Source code:** :source:`Lib/functools.py` 14 15 -------------- 13 16 14 17 The :mod:`functools` module is for higher-order functions: functions that act on … … 18 21 The :mod:`functools` module defines the following functions: 19 22 23 .. function:: cmp_to_key(func) 24 25 Transform an old-style comparison function to a key function. Used with 26 tools that accept key functions (such as :func:`sorted`, :func:`min`, 27 :func:`max`, :func:`heapq.nlargest`, :func:`heapq.nsmallest`, 28 :func:`itertools.groupby`). This function is primarily used as a transition 29 tool for programs being converted to Python 3 where comparison functions are 30 no longer supported. 31 32 A comparison function is any callable that accept two arguments, compares them, 33 and returns a negative number for less-than, zero for equality, or a positive 34 number for greater-than. A key function is a callable that accepts one 35 argument and returns another value that indicates the position in the desired 36 collation sequence. 37 38 Example:: 39 40 sorted(iterable, key=cmp_to_key(locale.strcoll)) # locale-aware sort order 41 42 .. versionadded:: 2.7 43 44 .. function:: total_ordering(cls) 45 46 Given a class defining one or more rich comparison ordering methods, this 47 class decorator supplies the rest. This simplifies the effort involved 48 in specifying all of the possible rich comparison operations: 49 50 The class must define one of :meth:`__lt__`, :meth:`__le__`, 51 :meth:`__gt__`, or :meth:`__ge__`. 52 In addition, the class should supply an :meth:`__eq__` method. 53 54 For example:: 55 56 @total_ordering 57 class Student: 58 def __eq__(self, other): 59 return ((self.lastname.lower(), self.firstname.lower()) == 60 (other.lastname.lower(), other.firstname.lower())) 61 def __lt__(self, other): 62 return ((self.lastname.lower(), self.firstname.lower()) < 63 (other.lastname.lower(), other.firstname.lower())) 64 65 .. versionadded:: 2.7 20 66 21 67 .. function:: reduce(function, iterable[, initializer])
Note:
See TracChangeset
for help on using the changeset viewer.