FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[merge from 3.5] - Issue27203 - Fix doctests Doc/faq/programming.rst. · pythoncapi/cpython@2950776 · GitHub

forked from python/cpython

Commit 2950776

Browse files
committed
[merge from 3.5] - Issue27203 - Fix doctests Doc/faq/programming.rst.
Patch contributed by Jelle Zijlstra.
2 parents 7a9ddd1 + 7749320 commit 2950776

1 file changed

Lines changed: 20 additions & 8 deletions

File tree

‎Doc/faq/programming.rst‎

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,16 +1172,28 @@ You probably tried to make a multidimensional array like this::
11721172

11731173
>>> A = [[None] * 2] * 3
11741174

1175-
This looks correct if you print it::
1175+
This looks correct if you print it:
1176+
1177+
.. testsetup::
1178+
1179+
A = [[None] * 2] * 3
1180+
1181+
.. doctest::
11761182

11771183
>>> A
11781184
[[None, None], [None, None], [None, None]]
11791185

11801186
But when you assign a value, it shows up in multiple places:
11811187

1182-
>>> A[0][0] = 5
1183-
>>> A
1184-
[[5, None], [5, None], [5, None]]
1188+
.. testsetup::
1189+
1190+
A = [[None] * 2] * 3
1191+
1192+
.. doctest::
1193+
1194+
>>> A[0][0] = 5
1195+
>>> A
1196+
[[5, None], [5, None], [5, None]]
11851197

11861198
The reason is that replicating a list with ``*`` doesn't create copies, it only
11871199
creates references to the existing objects. The ``*3`` creates a list
@@ -1665,9 +1677,9 @@ address, it happens frequently that after an object is deleted from memory, the
16651677
next freshly created object is allocated at the same position in memory. This
16661678
is illustrated by this example:
16671679

1668-
>>> id(1000)
1680+
>>> id(1000) # doctest: +SKIP
16691681
13901272
1670-
>>> id(2000)
1682+
>>> id(2000) # doctest: +SKIP
16711683
13901272
16721684

16731685
The two ids belong to different integer objects that are created before, and
@@ -1676,9 +1688,9 @@ objects whose id you want to examine are still alive, create another reference
16761688
to the object:
16771689

16781690
>>> a = 1000; b = 2000
1679-
>>> id(a)
1691+
>>> id(a) # doctest: +SKIP
16801692
13901272
1681-
>>> id(b)
1693+
>>> id(b) # doctest: +SKIP
16821694
13891296
16831695

16841696

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL