为什么有些对象会保持其身份,而其他对象在每次初始化时都会像这样改变?
>>> id(2 ** 850)
26826480
>>> id(2 ** 850)
26826480
>>> id(2 ** 850)
26826480
>>> id(2 ** 850)
26826480
>>> id(2 ** 851)
26826624
>>> id(2 ** 851)
26826480
>>> id(2 ** 851)
26826624
>>> id(2 ** 851)
26826480
>>> id(2 ** 851)
26826624
我还写了以下内容来寻找模式,但结果似乎毫无意义。我看不到图案。
def identifier():
ids = list()
i = 0
while i < 1000:
a = id(2 ** i)
b = id(2 ** i)
c = id(2 ** i)
d = id(2 ** i)
if a == b == c == d:
ids.append(i)
i += 1
print(ids)
identifier()
结果是
[0, 1, 2, 3, 4, 5, 6, 7, 8, 11, 18, 23, 24, 28, 31, 32, 33, 35, 37, 38, 39, 41, 42,
43, 44, 45, 47, 49, 50, 51, 52, 53, 55, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68,
69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90,
91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109,
110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 122, 124, 126, 128, 130, 132,
134, 136, 138, 140, 142, 144, 146, 148, 150, 151, 152, 153, 154, 155, 156, 157, 158,
159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175,
176, 177, 178, 179, 180, 182, 184, 186, 188, 190, 192, 194, 196, 198, 200, 202, 204,
206, 208, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224,
225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 242,
244, 246, 248, 250, 252, 254, 256, 258, 260, 262, 264, 266, 268, 270, 271, 272, 273,
274, 275, 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, 290,
291, 292, 293, 294, 295, 296, 297, 298, 299, 300, 302, 304, 306, 308, 310, 312, 314,
316, 318, 320, 322, 324, 326, 328, 330, 331, 332, 333, 334, 335, 336, 337, 338, 339,
340, 341, 342, 343, 344, 345, 346, 347, 348, 349, 350, 351, 352, 353, 354, 355, 356,
357, 358, 359, 360, 362, 364, 366, 368, 370, 372, 374, 376, 378, 380, 382, 384, 386,
388, 390, 391, 392, 393, 394, 395, 396, 397, 398, 399, 400, 401, 402, 403, 404, 405,
406, 407, 408, 409, 410, 411, 412, 413, 414, 415, 416, 417, 418, 419, 420, 422, 424,
426, 428, 430, 432, 434, 436, 438, 440, 442, 444, 446, 448, 450, 451, 452, 453, 454,
455, 456, 457, 458, 459, 460, 461, 462, 463, 464, 465, 466, 467, 468, 469, 470, 471,
472, 473, 474, 475, 476, 477, 478, 479, 480, 482, 484, 486, 488, 490, 492, 494, 496,
498, 500, 502, 504, 506, 508, 510, 511, 512, 513, 514, 515, 516, 517, 518, 519, 520,
521, 522, 523, 524, 525, 526, 527, 528, 529, 530, 531, 532, 533, 534, 535, 536, 537,
538, 539, 540, 542, 544, 546, 548, 550, 552, 554, 556, 558, 560, 562, 564, 566, 568,
570, 571, 572, 573, 574, 575, 576, 577, 578, 579, 580, 581, 582, 583, 584, 585, 586,
587, 588, 589, 590, 591, 592, 593, 594, 595, 596, 597, 598, 599, 600, 602, 604, 606,
608, 610, 612, 614, 616, 618, 620, 622, 624, 626, 628, 630, 631, 632, 633, 634, 635,
636, 637, 638, 639, 640, 641, 642, 643, 644, 645, 646, 647, 648, 649, 650, 651, 652,
653, 654, 655, 656, 657, 658, 659, 660, 662, 664, 666, 668, 670, 672, 674, 676, 678,
680, 682, 684, 686, 688, 690, 691, 692, 693, 694, 695, 696, 697, 698, 699, 700, 701,
702, 703, 704, 705, 706, 707, 708, 709, 710, 711, 712, 713, 714, 715, 716, 717, 718,
719, 720, 722, 724, 726, 728, 730, 732, 734, 736, 738, 740, 742, 744, 746, 748, 750,
751, 752, 753, 754, 755, 756, 757, 758, 759, 760, 761, 762, 763, 764, 765, 766, 767,
768, 769, 770, 771, 772, 773, 774, 775, 776, 777, 778, 779, 780, 782, 784, 786, 788,
790, 792, 794, 796, 798, 800, 802, 804, 806, 808, 810, 811, 812, 813, 814, 815, 816,
817, 818, 819, 820, 821, 822, 823, 824, 825, 826, 827, 828, 829, 830, 831, 832, 833,
834, 835, 836, 837, 838, 839, 840, 842, 844, 846, 848, 850, 852, 854, 856, 858, 860,
862, 864, 866, 868, 870, 871, 872, 873, 874, 875, 876, 877, 878, 879, 880, 881, 882,
883, 884, 885, 886, 887, 888, 889, 890, 891, 892, 893, 894, 895, 896, 897, 898, 899,
900, 902, 904, 906, 908, 910, 912, 914, 916, 918, 920, 922, 924, 926, 928, 930, 931,
932, 933, 934, 935, 936, 937, 938, 939, 940, 941, 942, 943, 944, 945, 946, 947, 948,
949, 950, 951, 952, 953, 954, 955, 956, 957, 958, 959, 960, 962, 964, 966, 968, 970,
972, 974, 976, 978, 980, 982, 984, 986, 988, 990, 991, 992, 993, 994, 995, 996, 997,
998, 999]
我可以看到为什么在 2 ** 8 之前存在一致性,因为 python 缓存了从 -5 到 256 的数字,但我无法理解之后的行为。有没有蟒蛇大师可以对此有所了解?
编辑:这种奇怪的行为激起了我的兴趣,所以我想了解其中的原因。我检查了邮件列表和源代码,但除了缓存小整数和分配 int 对象块来加速程序中分配和解除分配 int 对象的过程之外,它并没有说明什么。
>>> id(2 ** 950)
139686876237120
>>> id(2 ** 950)
139686876237120
>>> id(2 ** 951)
27205680
>>> id(2 ** 951)
27206160
>>> id(2 ** 951)
27206320
>>> id(2 ** 950)
139686876237120
>>> id(2 ** 950)
139686876237120
为什么在上面的第一个 int 对象被销毁后,它在最后一行重建时获得相同的内存地址?