| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1172,16 +1172,28 @@ You probably tried to make a multidimensional array like this:: | |||
| 1172 | 1172 | ||
| 1173 | 1173 | >>> A = [[None] * 2] * 3 | |
| 1174 | 1174 | ||
| 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:: | ||
| 1176 | 1182 | ||
| 1177 | 1183 | >>> A | |
| 1178 | 1184 | [[None, None], [None, None], [None, None]] | |
| 1179 | 1185 | ||
| 1180 | 1186 | But when you assign a value, it shows up in multiple places: | |
| 1181 | 1187 | ||
| 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]] | ||
| 1185 | 1197 | ||
| 1186 | 1198 | The reason is that replicating a list with ``*`` doesn't create copies, it only | |
| 1187 | 1199 | 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 | |||
| 1665 | 1677 | next freshly created object is allocated at the same position in memory. This | |
| 1666 | 1678 | is illustrated by this example: | |
| 1667 | 1679 | ||
| 1668 | - >>> id(1000) | ||
| 1680 | + >>> id(1000) # doctest: +SKIP | ||
| 1669 | 1681 | 13901272 | |
| 1670 | - >>> id(2000) | ||
| 1682 | + >>> id(2000) # doctest: +SKIP | ||
| 1671 | 1683 | 13901272 | |
| 1672 | 1684 | ||
| 1673 | 1685 | 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 | |||
| 1676 | 1688 | to the object: | |
| 1677 | 1689 | ||
| 1678 | 1690 | >>> a = 1000; b = 2000 | |
| 1679 | - >>> id(a) | ||
| 1691 | + >>> id(a) # doctest: +SKIP | ||
| 1680 | 1692 | 13901272 | |
| 1681 | - >>> id(b) | ||
| 1693 | + >>> id(b) # doctest: +SKIP | ||
| 1682 | 1694 | 13891296 | |
| 1683 | 1695 | ||
| 1684 | 1696 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments