Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f03f7c7

Browse files
committed
Closes #27204: Merge with 3.5
2 parents 1aa913e + 378a1d7 commit f03f7c7

3 files changed

Lines changed: 29 additions & 24 deletions

File tree

Doc/howto/functional.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1040,7 +1040,7 @@ If you use :func:`operator.add` with :func:`functools.reduce`, you'll add up all
10401040
elements of the iterable. This case is so common that there's a special
10411041
built-in called :func:`sum` to compute it:
10421042

1043-
>>> import functools
1043+
>>> import functools, operator
10441044
>>> functools.reduce(operator.add, [1,2,3,4], 0)
10451045
10
10461046
>>> sum([1,2,3,4])

Doc/howto/ipaddress.rst

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
.. testsetup::
2+
3+
import ipaddress
4+
15
.. _ipaddress-howto:
26

37
***************************************
@@ -49,11 +53,6 @@ to use the :func:`ipaddress.ip_address` factory function, which automatically
4953
determines whether to create an IPv4 or IPv6 address based on the passed in
5054
value:
5155

52-
.. testsetup::
53-
>>> import ipaddress
54-
55-
::
56-
5756
>>> ipaddress.ip_address('192.0.2.1')
5857
IPv4Address('192.0.2.1')
5958
>>> ipaddress.ip_address('2001:DB8::1')

Doc/howto/sorting.rst

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -58,28 +58,28 @@ A common pattern is to sort complex objects using some of the object's indices
5858
as keys. For example:
5959

6060
>>> student_tuples = [
61-
('john', 'A', 15),
62-
('jane', 'B', 12),
63-
('dave', 'B', 10),
64-
]
61+
... ('john', 'A', 15),
62+
... ('jane', 'B', 12),
63+
... ('dave', 'B', 10),
64+
... ]
6565
>>> sorted(student_tuples, key=lambda student: student[2]) # sort by age
6666
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
6767

6868
The same technique works for objects with named attributes. For example:
6969

7070
>>> class Student:
71-
def __init__(self, name, grade, age):
72-
self.name = name
73-
self.grade = grade
74-
self.age = age
75-
def __repr__(self):
76-
return repr((self.name, self.grade, self.age))
71+
... def __init__(self, name, grade, age):
72+
... self.name = name
73+
... self.grade = grade
74+
... self.age = age
75+
... def __repr__(self):
76+
... return repr((self.name, self.grade, self.age))
7777

7878
>>> student_objects = [
79-
Student('john', 'A', 15),
80-
Student('jane', 'B', 12),
81-
Student('dave', 'B', 10),
82-
]
79+
... Student('john', 'A', 15),
80+
... Student('jane', 'B', 12),
81+
... Student('dave', 'B', 10),
82+
... ]
8383
>>> sorted(student_objects, key=lambda student: student.age) # sort by age
8484
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
8585

@@ -208,15 +208,15 @@ return a negative value for less-than, return zero if they are equal, or return
208208
a positive value for greater-than. For example, we can do:
209209

210210
>>> def numeric_compare(x, y):
211-
return x - y
212-
>>> sorted([5, 2, 4, 1, 3], cmp=numeric_compare)
211+
... return x - y
212+
>>> sorted([5, 2, 4, 1, 3], cmp=numeric_compare) # doctest: +SKIP
213213
[1, 2, 3, 4, 5]
214214

215215
Or you can reverse the order of comparison with:
216216

217217
>>> def reverse_numeric(x, y):
218-
return y - x
219-
>>> sorted([5, 2, 4, 1, 3], cmp=reverse_numeric)
218+
... return y - x
219+
>>> sorted([5, 2, 4, 1, 3], cmp=reverse_numeric) # doctest: +SKIP
220220
[5, 4, 3, 2, 1]
221221

222222
When porting code from Python 2.x to 3.x, the situation can arise when you have
@@ -244,6 +244,12 @@ function. The following wrapper makes that easy to do::
244244

245245
To convert to a key function, just wrap the old comparison function:
246246

247+
.. testsetup::
248+
249+
from functools import cmp_to_key
250+
251+
.. doctest::
252+
247253
>>> sorted([5, 2, 4, 1, 3], key=cmp_to_key(reverse_numeric))
248254
[5, 4, 3, 2, 1]
249255

0 commit comments

Comments
 (0)