Skip to content

Commit f78d8d3

Browse files
committed
more appropriate test functions and better doc example
1 parent 2eacf5e commit f78d8d3

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

Doc/library/functools.rst

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -379,12 +379,15 @@ The :mod:`functools` module defines the following functions:
379379
:data:`!Placeholder` sentinel to the place held by previous :data:`!Placeholder`:
380380

381381
>>> from functools import partial, Placeholder as _
382-
>>> show5 = partial(print, _, _, _, 4)
383-
>>> show5 = partial(show5, _, _, 3)
384-
>>> show5 = partial(show5, _, 2)
385-
>>> show5 = partial(show5, _, 5) # 5 is appended after 4
386-
>>> show5(1)
387-
1 2 3 4 5
382+
>>> remove = partial(str.replace, _, _, '')
383+
>>> remove('Hello, world', 'l')
384+
'Heo, word'
385+
>>> remove_l = partial(remove, _, 'l')
386+
>>> remove_l('Hello, world')
387+
'Heo, word'
388+
>>> remove_first_l = partial(remove_l, _, 1)
389+
>>> remove_first_l('Hello, world')
390+
'Helo, world'
388391

389392
Note, :data:`!Placeholder` has no special treatment when used for keyword
390393
argument of :data:`!Placeholder`.

Lib/test/test_functools.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -222,22 +222,24 @@ def test_placeholders(self):
222222
args = (PH, 0)
223223
p = self.partial(capture, *args)
224224
got, empty = p('x')
225-
self.assertTrue(('x', 0) == got and empty == {})
225+
self.assertEqual(('x', 0), got)
226+
self.assertEqual(empty, {})
226227
# 2 Placeholders
227228
args = (PH, 0, PH, 1)
228229
p = self.partial(capture, *args)
229230
with self.assertRaises(TypeError):
230-
got, empty = p('x')
231+
p('x')
231232
got, empty = p('x', 'y')
232233
expected = ('x', 0, 'y', 1)
233-
self.assertTrue(expected == got and empty == {})
234+
self.assertEqual(expected, got)
235+
self.assertEqual(empty, {})
234236

235237
def test_placeholders_optimization(self):
236238
PH = self.module.Placeholder
237239
p = self.partial(capture, PH, 0)
238240
p2 = self.partial(p, PH, 1, 2, 3)
239241
expected = (PH, 0, 1, 2, 3)
240-
self.assertTrue(expected == p2.args)
242+
self.assertEqual(expected, p2.args)
241243
p3 = self.partial(p2, -1, 4)
242244
got, empty = p3(5)
243245
expected = (-1, 0, 1, 2, 3, 4, 5)
@@ -246,8 +248,8 @@ def test_placeholders_optimization(self):
246248
p = self.partial(capture, PH, 0)
247249
p2 = self.partial(p)
248250
expected = (PH, 0)
249-
self.assertTrue(expected == p2.args)
250-
self.assertTrue(((1, 0), {}) == p2(1))
251+
self.assertEqual(expected, p2.args)
252+
self.assertEqual(((1, 0), {}), p2(1))
251253

252254
def test_construct_placeholder_singleton(self):
253255
PH = self.module.Placeholder

0 commit comments

Comments
 (0)