Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1788944
  • 博文数量: 335
  • 博客积分: 4690
  • 博客等级: 上校
  • 技术积分: 4341
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-08 21:38
个人简介

无聊之人--除了技术,还是技术,你懂得

文章分类

全部博文(335)

文章存档

2016年(29)

2015年(18)

2014年(7)

2013年(86)

2012年(90)

2011年(105)

分类: Python/Ruby

2013-03-03 11:24:55

http://blog.chinaunix.net/uid-22334392-id-3497189.html中,我们给出了sizeof(PyIntObject)的大小为12

http://blog.chinaunix.net/uid-22334392-id-3495659.html中,我们总结了python所有的对象(包括对象,类型对象,类型对象的类型)的开始部分总是相同的,即具有如下的结构:


点击(此处)折叠或打开

  1. /* PyObject_HEAD defines the initial segment of every PyObject. */
  2. #define PyObject_HEAD
  3.     Py_ssize_t ob_refcnt;
  4.     struct _typeobject *ob_type;

一个int对象的结构如下:


点击(此处)折叠或打开

  1. typedef struct {
  2.     PyObject_HEAD
  3.     long ob_ival;
  4. } PyIntObject;

这一点是我们后续继续研究的基础,也是python源码分析的出发点。

int对象的class为PyIntObject,而该对象通过ob_type指针指向了该对象所说的类型对象PyInt_Type,而该对象PyInt_Type同样通过

ob_type指向了所有对象的基类:PyType_Type,PyType_Type则指向了自己。

在python源码分析这本书,指明了python整数对象的缓存机制,即对使用非常频繁的小整数是进行缓存的,小整数的范围是可以调整的,具体的范围在2.7中是可以重新调整的。


点击(此处)折叠或打开

  1. #define BLOCK_SIZE 1000 /* 1K less typical malloc overhead */
  2. #define BHEAD_SIZE 8 /* Enough for a 64-bit pointer */
  3. #define N_INTOBJECTS ((BLOCK_SIZE - BHEAD_SIZE) / sizeof(PyIntObject))

  4. struct _intblock {
  5.     struct _intblock *next;
  6.     PyIntObject objects[N_INTOBJECTS];
  7. };

  8. typedef struct _intblock PyIntBlock;

  9. static PyIntBlock *block_list = NULL;
  10. static PyIntObject *free_list = NULL;
  11. #ifndef NSMALLPOSINTS
  12. #define NSMALLPOSINTS 257
  13. #endif
  14. #ifndef NSMALLNEGINTS
  15. #define NSMALLNEGINTS 5
  16. #endif
  17. #if NSMALLNEGINTS + NSMALLPOSINTS > 0
  18. /* References to small integers are saved in this array so that they
  19.    can be shared.
  20.    The integers that are saved are those in the range
  21.    -NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive).
  22. */
  23. static PyIntObject *small_ints[NSMALLNEGINTS + NSMALLPOSINTS];
  24. #endif

通过使用PyIntBlock,python缓存了-5到257之间的所有整数,共262,这些对象都是放在PyIntBlock上的,一个PyIntBlock可以存放82个对象,因此在缓存262小整数的时候,python就需要创建4个PyIntBlock,这些PyIntBlock通过PyIntBlock的next指针进行连接,这都不是重点,我们重点研究的在每一个PyIntBlock,82个对象所形成的单向链表

点击(此处)折叠或打开

  1. fill_free_list(void)
  2. {
  3.     PyIntObject *p, *q;
  4.     /* Python's object allocator isn't appropriate for large blocks. */
  5.     p = (PyIntObject *) PyMem_MALLOC(sizeof(PyIntBlock));
  6.     if (p == NULL)
  7.         return (PyIntObject *) PyErr_NoMemory();
  8.     ((PyIntBlock *)p)->next = block_list;
  9.     block_list = (PyIntBlock *)p;
  10.     /* Link the int objects together, from rear to front, then return
  11.        the address of the last int object in the block. */
  12.     p = &((PyIntBlock *)p)->objects[0];
  13.     q = p + N_INTOBJECTS;
  14.     while (--q > p)
  15.         Py_TYPE(q) = (struct _typeobject *)(q-1);
  16.     Py_TYPE(q) = NULL;
  17.     return p + N_INTOBJECTS - 1;
  18. }
  19. PyObject *
  20. PyInt_FromLong(long ival)
  21. {
  22.     register PyIntObject *v;
  23. #if NSMALLNEGINTS + NSMALLPOSINTS > 0
  24.     if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) {
  25.         v = small_ints[ival + NSMALLNEGINTS];
  26.         Py_INCREF(v);
  27. #ifdef COUNT_ALLOCS
  28.         if (ival >= 0)
  29.             quick_int_allocs++;
  30.         else
  31.             quick_neg_int_allocs++;
  32. #endif
  33.         return (PyObject *) v;
  34.     }
  35. #endif
  36.     if (free_list == NULL) {
  37.         if ((free_list = fill_free_list()) == NULL)
  38.             return NULL;
  39.     }
  40.     /* Inline PyObject_New */
  41.     v = free_list;
  42.     free_list = (PyIntObject *)Py_TYPE(v);
  43.     PyObject_INIT(v, &PyInt_Type);
  44.     v->ob_ival = ival;
  45.     return (PyObject *) v;
  46. }
从这两个函数,我们可以清晰的看到,小整数确实是缓存到了PyIntBlock,为了验证确实如此,我们修改print_int代码后,进行

确认:

点击(此处)折叠或打开

  1. /*add by kinfinger */
  2.  435 static void print_block(PyIntBlock * pblock,int n){
  3.  436 static PyIntObject *freelist,*qfree;
  4.  437 freelist = free_list;
  5.  438
  6.  439 PyIntObject * pint, *qint;
  7.  440 pint =&pblock->objects[0];
  8.  441 qint = pint + N_INTOBJECTS;
  9.  442 while( --qint >= pint){
  10.  443 /* if (qint == freelist ) blank block
  11.  444 freelist = (PyIntObject *) freelist->ob_type;
  12.  445 else */
  13.  446 printf("value %8ld at @ %p with ref count %4d in %4d IN TBLOCK n", qint->ob_ival,&(qint->ob_ival),qint->ob_refcnt,n);
  14.  447 }
  15.  448 }

  16. static int
  17.  451 int_print(PyIntObject *v, FILE *fp, int flags)
  18.  452 /* flags -- not used but required by interface */
  19.  453 {
  20.  454 long int_val = v->ob_ival;
  21.  455 Py_BEGIN_ALLOW_THREADS
  22.  456 fprintf(fp, "%ld", int_val);
  23.  457 printf("nthe below is something new n");
  24.  458 /*******add by kinfinger */
  25.  459 
  26.  460 PyIntBlock * pblock,*qblock;
  27.  461 pblock = block_list;
  28.  462 int n = 0;
  29.  463 while(pblock){
  30.  464 n += 1;
  31.  465 qblock = pblock->next;
  32.  466 print_block(pblock,n);
  33.  467 pblock = qblock;
  34.  468 }
  35.  469 printf("the free_list add is %pn",&(free_list->ob_ival));
  36.  470
  37.  471 Py_END_ALLOW_THREADS
  38.  472 return 0
然后重新编译,运行后得到如下结果:

点击(此处)折叠或打开

  1. the below is something new
  2. value 2097152 at @ 0x9bdd710 with ref count 1 in 1 INTBLOCK
  3. value 1927868237 at @ 0x9bdd704 with ref count 1 in 1 INTBLOCK
  4. value 89869747 at @ 0x9bdd6f8 with ref count 1 in 1 INTBLOCK
  5. value 69069 at @ 0x9bdd6ec with ref count 1 in 1 INTBLOCK
  6. value 907133923 at @ 0x9bdd6e0 with ref count 1 in 1 INTBLOCK
  7. value 590923713 at @ 0x9bdd6d4 with ref count 1 in 1 INTBLOCK
  8. value 2147483647 at @ 0x9bdd6c8 with ref count 1 in 1 INTBLOCK
  9. value 512 at @ 0x9bdd6bc with ref count 1 in 1 INTBLOCK
  10. value 755 at @ 0x9bdd6b0 with ref count 1 in 1 INTBLOCK
  11. value 65535 at @ 0x9bdd6a4 with ref count 1 in 1 INTBLOCK
  12. value 65536 at @ 0x9bdd698 with ref count 1 in 1 INTBLOCK
  13. value 2147483647 at @ 0x9bdd68c with ref count 1 in 1 INTBLOCK
  14. value 20031017 at @ 0x9bdd680 with ref count 2 in 1 INTBLOCK
  15. value 20031017 at @ 0x9bdd674 with ref count 3 in 1 INTBLOCK
  16. value 65535 at @ 0x9bdd668 with ref count 3 in 1 INTBLOCK
  17. value 644 at @ 0x9bdd65c with ref count 1 in 1 INTBLOCK
  18. value 1000 at @ 0x9bdd650 with ref count 0 in 1 INTBLOCK
  19. value 65535 at @ 0x9bdd644 with ref count 1 in 1 INTBLOCK
  20. value 163720712 at @ 0x9bdd638 with ref count 0 in 1 INTBLOCK
  21. value 8000 at @ 0x9bdd62c with ref count 1 in 1 INTBLOCK
  22. value 1362222303 at @ 0x9bdd620 with ref count 0 in 1 INTBLOCK
  23. value 755 at @ 0x9bdd614 with ref count 1 in 1 INTBLOCK
  24. value 32768 at @ 0x9bdd608 with ref count 0 in 1 INTBLOCK
  25. value 16384 at @ 0x9bdd5fc with ref count 0 in 1 INTBLOCK
  26. value 163720712 at @ 0x9bdd5f0 with ref count 0 in 1 INTBLOCK
  27. value 16877 at @ 0x9bdd5e4 with ref count 0 in 1 INTBLOCK
  28. value 1000 at @ 0x9bdd5d8 with ref count 0 in 1 INTBLOCK
  29. value 32768 at @ 0x9bdd5cc with ref count 0 in 1 INTBLOCK
  30. value 1698967413 at @ 0x9bdd5c0 with ref count 1966040429 in 1 INTBLOCK
  31. value 1869098752 at @ 0x9bdd5b4 with ref count 0 in 1 INTBLOCK
  32. value 671088640 at @ 0x9bdd5a8 with ref count 4674048 in 1 INTBLOCK
  33. value 18002 at @ 0x9bdd59c with ref count 884 in 1 INTBLOCK
  34. value 829449728 at @ 0x9bdd590 with ref count 4 in 1 INTBLOCK
  35. value 671088640 at @ 0x9bdd584 with ref count 1953719808 in 1 INTBLOCK
  36. value 1396 at @ 0x9bdd578 with ref count 1375731712 in 1 INTBLOCK
  37. value 52979052 at @ 0x9bdd56c with ref count 1835103008 in 1 INTBLOCK
  38. value 1701344288 at @ 0x9bdd560 with ref count 1919247973 in 1 INTBLOCK
  39. value 1914729332 at @ 0x9bdd554 with ref count 1864394092 in 1 INTBLOCK
  40. value 1768300654 at @ 0x9bdd548 with ref count 1870099488 in 1 INTBLOCK
  41. value 1919248500 at @ 0x9bdd53c with ref count 1953719636 in 1 INTBLOCK
  42. value 58 at @ 0x9bdd530 with ref count 19419904 in 1 INTBLOCK
  43. value 42139651 at @ 0x9bdd524 with ref count 160768 in 1 INTBLOCK
  44. value 58523649 at @ 0x9bdd518 with ref count 92672 in 1 INTBLOCK
  45. value 7602178 at @ 0x9bdd50c with ref count 31744 in 1 INTBLOCK
  46. value 23724032 at @ 0x9bdd500 with ref count 728956928 in 1 INTBLOCK
  47. value 4390912 at @ 0x9bdd4f4 with ref count 262144 in 1 INTBLOCK
  48. value 156417 at @ 0x9bdd4e8 with ref count 0 in 1 INTBLOCK
  49. value 108199936 at @ 0x9bdd4dc with ref count 1659513 in 1 INTBLOCK
  50. value 1882089588 at @ 0x9bdd4d0 with ref count 1936683055 in 1 INTBLOCK
  51. value 1651067951 at @ 0x9bdd4c4 with ref count 1885957737 in 1 INTBLOCK
  52. value 1831822194 at @ 0x9bdd4b8 with ref count 1664053359 in 1 INTBLOCK
  53. value 1953198949 at @ 0x9bdd4ac with ref count 1853186677 in 1 INTBLOCK
  54. value 795176303 at @ 0x9bdd4a0 with ref count 3633920 in 1 INTBLOCK
  55. value 40 at @ 0x9bdd494 with ref count 674394880 in 1 INTBLOCK
  56. value 628 at @ 0x9bdd488 with ref count 160818 in 1 INTBLOCK
  57. value 1711276032 at @ 0x9bdd47c with ref count 2 in 1 INTBLOCK
  58. value 1946157056 at @ 0x9bdd470 with ref count 1790464 in 1 INTBLOCK
  59. value 16210 at @ 0x9bdd464 with ref count 1375731712 in 1 INTBLOCK
  60. value 52979052 at @ 0x9bdd458 with ref count 1635087459 in 1 INTBLOCK
  61. value 1629513069 at @ 0x9bdd44c with ref count 1752440933 in 1 INTBLOCK
  62. value 1668179314 at @ 0x9bdd440 with ref count 544433517 in 1 INTBLOCK
  63. value 1634625652 at @ 0x9bdd434 with ref count 2004099186 in 1 INTBLOCK
  64. value 1701344357 at @ 0x9bdd428 with ref count 1936020480 in 1 INTBLOCK
  65. value 14707 at @ 0x9bdd41c with ref count 676528130 in 1 INTBLOCK
  66. value -2097151108 at @ 0x9bdd410 with ref count 41156611 in 1 INTBLOCK
  67. value 2097152387 at @ 0x9bdd404 with ref count 23724032 in 1 INTBLOCK
  68. value 1946157693 at @ 0x9bdd3f8 with ref count 8126465 in 1 INTBLOCK
  69. value 1778385012 at @ 0x9bdd3ec with ref count 1929379840 in 1 INTBLOCK
  70. value 1124073472 at @ 0x9bdd3e0 with ref count 67108864 in 1 INTBLOCK
  71. value 40042757 at @ 0x9bdd3d4 with ref count 16974336 in 1 INTBLOCK
  72. value 10 at @ 0x9bdd3c8 with ref count -1946157056 in 1 INTBLOCK
  73. value 273840496 at @ 0x9bdd3bc with ref count 1886939507 in 1 INTBLOCK
  74. value 1869623138 at @ 0x9bdd3b0 with ref count 774863216 in 1 INTBLOCK
  75. value 1768843629 at @ 0x9bdd3a4 with ref count 796487779 in 1 INTBLOCK
  76. value 795897716 at @ 0x9bdd398 with ref count 796226670 in 1 INTBLOCK
  77. value 1969386799 at @ 0x9bdd38c with ref count 788529152 in 1 INTBLOCK
  78. value 930283520 at @ 0x9bdd380 with ref count 40 in 1 INTBLOCK
  79. value 48 at @ 0x9bdd374 with ref count 19424629 in 1 INTBLOCK
  80. value 1918107648 at @ 0x9bdd368 with ref count 15954 in 1 INTBLOCK
  81. value 60 at @ 0x9bdd35c with ref count 995229696 in 1 INTBLOCK
  82. value 3822080 at @ 0x9bdd350 with ref count 1936420457 in 1 INTBLOCK
  83. value 163435312 at @ 0x9bdd344 with ref count 7808880 in 1 INTBLOCK
  84. value 241 at @ 0x9bbaca0 with ref count 1 in 2 INTBLOCK
  85. value 242 at @ 0x9bbac94 with ref count 1 in 2 INTBLOCK
  86. value 243 at @ 0x9bbac88 with ref count 1 in 2 INTBLOCK
  87. value 244 at @ 0x9bbac7c with ref count 1 in 2 INTBLOCK
  88. value 245 at @ 0x9bbac70 with ref count 1 in 2 INTBLOCK
  89. value 246 at @ 0x9bbac64 with ref count 1 in 2 INTBLOCK
  90. value 247 at @ 0x9bbac58 with ref count 1 in 2 INTBLOCK
  91. value 248 at @ 0x9bbac4c with ref count 1 in 2 INTBLOCK
  92. value 249 at @ 0x9bbac40 with ref count 1 in 2 INTBLOCK
  93. value 250 at @ 0x9bbac34 with ref count 1 in 2 INTBLOCK
  94. value 251 at @ 0x9bbac28 with ref count 1 in 2 INTBLOCK
  95. value 252 at @ 0x9bbac1c with ref count 1 in 2 INTBLOCK
  96. value 253 at @ 0x9bbac10 with ref count 1 in 2 INTBLOCK
  97. value 254 at @ 0x9bbac04 with ref count 1 in 2 INTBLOCK
  98. value 255 at @ 0x9bbabf8 with ref count 4 in 2 INTBLOCK
  99. value 256 at @ 0x9bbabec with ref count 8 in 2 INTBLOCK
  100. value 34014192 at @ 0x9bbabe0 with ref count 2 in 2 INTBLOCK
  101. value 1013 at @ 0x9bbabd4 with ref count 2 in 2 INTBLOCK
  102. value 2147483647 at @ 0x9bbabc8 with ref count 2 in 2 INTBLOCK
  103. value 2147483647 at @ 0x9bbabbc with ref count 2 in 2 INTBLOCK
  104. value 1024 at @ 0x9bbabb0 with ref count 1 in 2 INTBLOCK
  105. value 308 at @ 0x9bbaba4 with ref count 1 in 2 INTBLOCK
  106. value -1021 at @ 0x9bbab98 with ref count 1 in 2 INTBLOCK
  107. value -307 at @ 0x9bbab8c with ref count 1 in 2 INTBLOCK
  108. value 65535 at @ 0x9bbab80 with ref count 2 in 2 INTBLOCK
  109. value 511 at @ 0x9bbab74 with ref count 1 in 2 INTBLOCK
  110. value 65536 at @ 0x9bbab68 with ref count 3 in 2 INTBLOCK
  111. value 238328 at @ 0x9bbab5c with ref count 3 in 2 INTBLOCK
  112. value 2048 at @ 0x9bbab50 with ref count 3 in 2 INTBLOCK
  113. value 2048 at @ 0x9bbab44 with ref count 3 in 2 INTBLOCK
  114. value 1024 at @ 0x9bbab38 with ref count 3 in 2 INTBLOCK
  115. value 4096 at @ 0x9bbab2c with ref count 3 in 2 INTBLOCK
  116. value 1052672 at @ 0x9bbab20 with ref count 3 in 2 INTBLOCK
  117. value 1052672 at @ 0x9bbab14 with ref count 3 in 2 INTBLOCK
  118. value 512 at @ 0x9bbab08 with ref count 3 in 2 INTBLOCK
  119. value 32768 at @ 0x9bbaafc with ref count 3 in 2 INTBLOCK
  120. value 8192 at @ 0x9bbaaf0 with ref count 3 in 2 INTBLOCK
  121. value 16384 at @ 0x9bbaae4 with ref count 3 in 2 INTBLOCK
  122. value 65536 at @ 0x9bbaad8 with ref count 3 in 2 INTBLOCK
  123. value 131072 at @ 0x9bbaacc with ref count 3 in 2 INTBLOCK
  124. value 262144 at @ 0x9bbaac0 with ref count 3 in 2 INTBLOCK
  125. value 1004 at @ 0x9bbaab4 with ref count 1 in 2 INTBLOCK
  126. value 1005 at @ 0x9bbaaa8 with ref count 1 in 2 INTBLOCK
  127. value 1006 at @ 0x9bbaa9c with ref count 1 in 2 INTBLOCK
  128. value 1007 at @ 0x9bbaa90 with ref count 1 in 2 INTBLOCK
  129. value 1000 at @ 0x9bbaa84 with ref count 1 in 2 INTBLOCK
  130. value 1001 at @ 0x9bbaa78 with ref count 1 in 2 INTBLOCK
  131. value 1002 at @ 0x9bbaa6c with ref count 1 in 2 INTBLOCK
  132. value 1003 at @ 0x9bbaa60 with ref count 1 in 2 INTBLOCK
  133. value 1100 at @ 0x9bbaa54 with ref count 1 in 2 INTBLOCK
  134. value 1101 at @ 0x9bbaa48 with ref count 1 in 2 INTBLOCK
  135. value 1102 at @ 0x9bbaa3c with ref count 1 in 2 INTBLOCK
  136. value 1103 at @ 0x9bbaa30 with ref count 1 in 2 INTBLOCK
  137. value 1104 at @ 0x9bbaa24 with ref count 1 in 2 INTBLOCK
  138. value 1105 at @ 0x9bbaa18 with ref count 1 in 2 INTBLOCK
  139. value 1106 at @ 0x9bbaa0c with ref count 1 in 2 INTBLOCK
  140. value 1107 at @ 0x9bbaa00 with ref count 1 in 2 INTBLOCK
  141. value 1108 at @ 0x9bba9f4 with ref count 1 in 2 INTBLOCK
  142. value 1109 at @ 0x9bba9e8 with ref count 1 in 2 INTBLOCK
  143. value 1110 at @ 0x9bba9dc with ref count 1 in 2 INTBLOCK
  144. value 1111 at @ 0x9bba9d0 with ref count 1 in 2 INTBLOCK
  145. value 1112 at @ 0x9bba9c4 with ref count 1 in 2 INTBLOCK
  146. value 1113 at @ 0x9bba9b8 with ref count 1 in 2 INTBLOCK
  147. value 1114 at @ 0x9bba9ac with ref count 1 in 2 INTBLOCK
  148. value 1115 at @ 0x9bba9a0 with ref count 1 in 2 INTBLOCK
  149. value 4095 at @ 0x9bba994 with ref count 1 in 2 INTBLOCK
  150. value 61440 at @ 0x9bba988 with ref count 1 in 2 INTBLOCK
  151. value 16384 at @ 0x9bba97c with ref count 1 in 2 INTBLOCK
  152. value 8192 at @ 0x9bba970 with ref count 1 in 2 INTBLOCK
  153. value 24576 at @ 0x9bba964 with ref count 1 in 2 INTBLOCK
  154. value 32768 at @ 0x9bba958 with ref count 2 in 2 INTBLOCK
  155. value 4096 at @ 0x9bba94c with ref count 1 in 2 INTBLOCK
  156. value 40960 at @ 0x9bba940 with ref count 1 in 2 INTBLOCK
  157. value 49152 at @ 0x9bba934 with ref count 1 in 2 INTBLOCK
  158. value 2048 at @ 0x9bba928 with ref count 1 in 2 INTBLOCK
  159. value 1024 at @ 0x9bba91c with ref count 2 in 2 INTBLOCK
  160. value 512 at @ 0x9bba910 with ref count 1 in 2 INTBLOCK
  161. value 448 at @ 0x9bba904 with ref count 1 in 2 INTBLOCK
  162. value 65536 at @ 0x9bba8f8 with ref count 1 in 2 INTBLOCK
  163. value 131072 at @ 0x9bba8ec with ref count 1 in 2 INTBLOCK
  164. value 262144 at @ 0x9bba8e0 with ref count 1 in 2 INTBLOCK
  165. value 1048576 at @ 0x9bba8d4 with ref count 1 in 2 INTBLOCK
  166. value 159 at @ 0x9bba8c0 with ref count 1 in 3 INTBLOCK
  167. value 160 at @ 0x9bba8b4 with ref count 1 in 3 INTBLOCK
  168. value 161 at @ 0x9bba8a8 with ref count 1 in 3 INTBLOCK
  169. value 162 at @ 0x9bba89c with ref count 1 in 3 INTBLOCK
  170. value 163 at @ 0x9bba890 with ref count 1 in 3 INTBLOCK
  171. value 164 at @ 0x9bba884 with ref count 1 in 3 INTBLOCK
  172. value 165 at @ 0x9bba878 with ref count 1 in 3 INTBLOCK
  173. value 166 at @ 0x9bba86c with ref count 1 in 3 INTBLOCK
  174. value 167 at @ 0x9bba860 with ref count 1 in 3 INTBLOCK
  175. value 168 at @ 0x9bba854 with ref count 1 in 3 INTBLOCK
  176. value 169 at @ 0x9bba848 with ref count 1 in 3 INTBLOCK
  177. value 170 at @ 0x9bba83c with ref count 1 in 3 INTBLOCK
  178. value 171 at @ 0x9bba830 with ref count 1 in 3 INTBLOCK
  179. value 172 at @ 0x9bba824 with ref count 1 in 3 INTBLOCK
  180. value 173 at @ 0x9bba818 with ref count 1 in 3 INTBLOCK
  181. value 174 at @ 0x9bba80c with ref count 1 in 3 INTBLOCK
  182. value 175 at @ 0x9bba800 with ref count 1 in 3 INTBLOCK
  183. value 176 at @ 0x9bba7f4 with ref count 1 in 3 INTBLOCK
  184. value 177 at @ 0x9bba7e8 with ref count 1 in 3 INTBLOCK
  185. value 178 at @ 0x9bba7dc with ref count 1 in 3 INTBLOCK
  186. value 179 at @ 0x9bba7d0 with ref count 1 in 3 INTBLOCK
  187. value 180 at @ 0x9bba7c4 with ref count 1 in 3 INTBLOCK
  188. value 181 at @ 0x9bba7b8 with ref count 1 in 3 INTBLOCK
  189. value 182 at @ 0x9bba7ac with ref count 1 in 3 INTBLOCK
  190. value 183 at @ 0x9bba7a0 with ref count 1 in 3 INTBLOCK
  191. value 184 at @ 0x9bba794 with ref count 1 in 3 INTBLOCK
  192. value 185 at @ 0x9bba788 with ref count 1 in 3 INTBLOCK
  193. value 186 at @ 0x9bba77c with ref count 1 in 3 INTBLOCK
  194. value 187 at @ 0x9bba770 with ref count 1 in 3 INTBLOCK
  195. value 188 at @ 0x9bba764 with ref count 1 in 3 INTBLOCK
  196. value 189 at @ 0x9bba758 with ref count 1 in 3 INTBLOCK
  197. value 190 at @ 0x9bba74c with ref count 1 in 3 INTBLOCK
  198. value 191 at @ 0x9bba740 with ref count 1 in 3 INTBLOCK
  199. value 192 at @ 0x9bba734 with ref count 1 in 3 INTBLOCK
  200. value 193 at @ 0x9bba728 with ref count 1 in 3 INTBLOCK
  201. value 194 at @ 0x9bba71c with ref count 1 in 3 INTBLOCK
  202. value 195 at @ 0x9bba710 with ref count 1 in 3 INTBLOCK
  203. value 196 at @ 0x9bba704 with ref count 1 in 3 INTBLOCK
  204. value 197 at @ 0x9bba6f8 with ref count 1 in 3 INTBLOCK
  205. value 198 at @ 0x9bba6ec with ref count 1 in 3 INTBLOCK
  206. value 199 at @ 0x9bba6e0 with ref count 1 in 3 INTBLOCK
  207. value 200 at @ 0x9bba6d4 with ref count 1 in 3 INTBLOCK
  208. value 201 at @ 0x9bba6c8 with ref count 1 in 3 INTBLOCK
  209. value 202 at @ 0x9bba6bc with ref count 1 in 3 INTBLOCK
  210. value 203 at @ 0x9bba6b0 with ref count 1 in 3 INTBLOCK
  211. value 204 at @ 0x9bba6a4 with ref count 1 in 3 INTBLOCK
  212. value 205 at @ 0x9bba698 with ref count 1 in 3 INTBLOCK
  213. value 206 at @ 0x9bba68c with ref count 1 in 3 INTBLOCK
  214. value 207 at @ 0x9bba680 with ref count 1 in 3 INTBLOCK
  215. value 208 at @ 0x9bba674 with ref count 1 in 3 INTBLOCK
  216. value 209 at @ 0x9bba668 with ref count 1 in 3 INTBLOCK
  217. value 210 at @ 0x9bba65c with ref count 1 in 3 INTBLOCK
  218. value 211 at @ 0x9bba650 with ref count 1 in 3 INTBLOCK
  219. value 212 at @ 0x9bba644 with ref count 1 in 3 INTBLOCK
  220. value 213 at @ 0x9bba638 with ref count 1 in 3 INTBLOCK
  221. value 214 at @ 0x9bba62c with ref count 1 in 3 INTBLOCK
  222. value 215 at @ 0x9bba620 with ref count 1 in 3 INTBLOCK
  223. value 216 at @ 0x9bba614 with ref count 1 in 3 INTBLOCK
  224. value 217 at @ 0x9bba608 with ref count 1 in 3 INTBLOCK
  225. value 218 at @ 0x9bba5fc with ref count 1 in 3 INTBLOCK
  226. value 219 at @ 0x9bba5f0 with ref count 1 in 3 INTBLOCK
  227. value 220 at @ 0x9bba5e4 with ref count 1 in 3 INTBLOCK
  228. value 221 at @ 0x9bba5d8 with ref count 1 in 3 INTBLOCK
  229. value 222 at @ 0x9bba5cc with ref count 1 in 3 INTBLOCK
  230. value 223 at @ 0x9bba5c0 with ref count 1 in 3 INTBLOCK
  231. value 224 at @ 0x9bba5b4 with ref count 1 in 3 INTBLOCK
  232. value 225 at @ 0x9bba5a8 with ref count 1 in 3 INTBLOCK
  233. value 226 at @ 0x9bba59c with ref count 1 in 3 INTBLOCK
  234. value 227 at @ 0x9bba590 with ref count 1 in 3 INTBLOCK
  235. value 228 at @ 0x9bba584 with ref count 1 in 3 INTBLOCK
  236. value 229 at @ 0x9bba578 with ref count 1 in 3 INTBLOCK
  237. value 230 at @ 0x9bba56c with ref count 1 in 3 INTBLOCK
  238. value 231 at @ 0x9bba560 with ref count 1 in 3 INTBLOCK
  239. value 232 at @ 0x9bba554 with ref count 1 in 3 INTBLOCK
  240. value 233 at @ 0x9bba548 with ref count 1 in 3 INTBLOCK
  241. value 234 at @ 0x9bba53c with ref count 1 in 3 INTBLOCK
  242. value 235 at @ 0x9bba530 with ref count 1 in 3 INTBLOCK
  243. value 236 at @ 0x9bba524 with ref count 1 in 3 INTBLOCK
  244. value 237 at @ 0x9bba518 with ref count 1 in 3 INTBLOCK
  245. value 238 at @ 0x9bba50c with ref count 1 in 3 INTBLOCK
  246. value 239 at @ 0x9bba500 with ref count 1 in 3 INTBLOCK
  247. value 240 at @ 0x9bba4f4 with ref count 1 in 3 INTBLOCK
  248. value 77 at @ 0x9bba4e0 with ref count 8 in 4 INTBLOCK
  249. value 78 at @ 0x9bba4d4 with ref count 8 in 4 INTBLOCK
  250. value 79 at @ 0x9bba4c8 with ref count 5 in 4 INTBLOCK
  251. value 80 at @ 0x9bba4bc with ref count 5 in 4 INTBLOCK
  252. value 81 at @ 0x9bba4b0 with ref count 5 in 4 INTBLOCK
  253. value 82 at @ 0x9bba4a4 with ref count 5 in 4 INTBLOCK
  254. value 83 at @ 0x9bba498 with ref count 5 in 4 INTBLOCK
  255. value 84 at @ 0x9bba48c with ref count 5 in 4 INTBLOCK
  256. value 85 at @ 0x9bba480 with ref count 5 in 4 INTBLOCK
  257. value 86 at @ 0x9bba474 with ref count 5 in 4 INTBLOCK
  258. value 87 at @ 0x9bba468 with ref count 5 in 4 INTBLOCK
  259. value 88 at @ 0x9bba45c with ref count 5 in 4 INTBLOCK
  260. value 89 at @ 0x9bba450 with ref count 5 in 4 INTBLOCK
  261. value 90 at @ 0x9bba444 with ref count 5 in 4 INTBLOCK
  262. value 91 at @ 0x9bba438 with ref count 5 in 4 INTBLOCK
  263. value 92 at @ 0x9bba42c with ref count 6 in 4 INTBLOCK
  264. value 93 at @ 0x9bba420 with ref count 5 in 4 INTBLOCK
  265. value 94 at @ 0x9bba414 with ref count 5 in 4 INTBLOCK
  266. value 95 at @ 0x9bba408 with ref count 5 in 4 INTBLOCK
  267. value 96 at @ 0x9bba3fc with ref count 5 in 4 INTBLOCK
  268. value 97 at @ 0x9bba3f0 with ref count 5 in 4 INTBLOCK
  269. value 98 at @ 0x9bba3e4 with ref count 5 in 4 INTBLOCK
  270. value 99 at @ 0x9bba3d8 with ref count 5 in 4 INTBLOCK
  271. value 100 at @ 0x9bba3cc with ref count 7 in 4 INTBLOCK
  272. value 101 at @ 0x9bba3c0 with ref count 5 in 4 INTBLOCK
  273. value 102 at @ 0x9bba3b4 with ref count 5 in 4 INTBLOCK
  274. value 103 at @ 0x9bba3a8 with ref count 5 in 4 INTBLOCK
  275. value 104 at @ 0x9bba39c with ref count 5 in 4 INTBLOCK
  276. value 105 at @ 0x9bba390 with ref count 5 in 4 INTBLOCK
  277. value 106 at @ 0x9bba384 with ref count 5 in 4 INTBLOCK
  278. value 107 at @ 0x9bba378 with ref count 5 in 4 INTBLOCK
  279. value 108 at @ 0x9bba36c with ref count 5 in 4 INTBLOCK
  280. value 109 at @ 0x9bba360 with ref count 5 in 4 INTBLOCK
  281. value 110 at @ 0x9bba354 with ref count 5 in 4 INTBLOCK
  282. value 111 at @ 0x9bba348 with ref count 5 in 4 INTBLOCK
  283. value 112 at @ 0x9bba33c with ref count 5 in 4 INTBLOCK
  284. value 113 at @ 0x9bba330 with ref count 5 in 4 INTBLOCK
  285. value 114 at @ 0x9bba324 with ref count 5 in 4 INTBLOCK
  286. value 115 at @ 0x9bba318 with ref count 5 in 4 INTBLOCK
  287. value 116 at @ 0x9bba30c with ref count 5 in 4 INTBLOCK
  288. value 117 at @ 0x9bba300 with ref count 5 in 4 INTBLOCK
  289. value 118 at @ 0x9bba2f4 with ref count 5 in 4 INTBLOCK
  290. value 119 at @ 0x9bba2e8 with ref count 5 in 4 INTBLOCK
  291. value 120 at @ 0x9bba2dc with ref count 5 in 4 INTBLOCK
  292. value 121 at @ 0x9bba2d0 with ref count 5 in 4 INTBLOCK
  293. value 122 at @ 0x9bba2c4 with ref count 5 in 4 INTBLOCK
  294. value 123 at @ 0x9bba2b8 with ref count 2 in 4 INTBLOCK
  295. value 124 at @ 0x9bba2ac with ref count 2 in 4 INTBLOCK
  296. value 125 at @ 0x9bba2a0 with ref count 2 in 4 INTBLOCK
  297. value 126 at @ 0x9bba294 with ref count 2 in 4 INTBLOCK
  298. value 127 at @ 0x9bba288 with ref count 3 in 4 INTBLOCK
  299. value 128 at @ 0x9bba27c with ref count 11 in 4 INTBLOCK
  300. value 129 at @ 0x9bba270 with ref count 2 in 4 INTBLOCK
  301. value 130 at @ 0x9bba264 with ref count 2 in 4 INTBLOCK
  302. value 131 at @ 0x9bba258 with ref count 2 in 4 INTBLOCK
  303. value 132 at @ 0x9bba24c with ref count 1 in 4 INTBLOCK
  304. value 133 at @ 0x9bba240 with ref count 1 in 4 INTBLOCK
  305. value 134 at @ 0x9bba234 with ref count 1 in 4 INTBLOCK
  306. value 135 at @ 0x9bba228 with ref count 1 in 4 INTBLOCK
  307. value 136 at @ 0x9bba21c with ref count 1 in 4 INTBLOCK
  308. value 137 at @ 0x9bba210 with ref count 1 in 4 INTBLOCK
  309. value 138 at @ 0x9bba204 with ref count 1 in 4 INTBLOCK
  310. value 139 at @ 0x9bba1f8 with ref count 1 in 4 INTBLOCK
  311. value 140 at @ 0x9bba1ec with ref count 1 in 4 INTBLOCK
  312. value 141 at @ 0x9bba1e0 with ref count 1 in 4 INTBLOCK
  313. value 142 at @ 0x9bba1d4 with ref count 1 in 4 INTBLOCK
  314. value 143 at @ 0x9bba1c8 with ref count 1 in 4 INTBLOCK
  315. value 144 at @ 0x9bba1bc with ref count 1 in 4 INTBLOCK
  316. value 145 at @ 0x9bba1b0 with ref count 1 in 4 INTBLOCK
  317. value 146 at @ 0x9bba1a4 with ref count 1 in 4 INTBLOCK
  318. value 147 at @ 0x9bba198 with ref count 1 in 4 INTBLOCK
  319. value 148 at @ 0x9bba18c with ref count 1 in 4 INTBLOCK
  320. value 149 at @ 0x9bba180 with ref count 1 in 4 INTBLOCK
  321. value 150 at @ 0x9bba174 with ref count 1 in 4 INTBLOCK
  322. value 151 at @ 0x9bba168 with ref count 1 in 4 INTBLOCK
  323. value 152 at @ 0x9bba15c with ref count 1 in 4 INTBLOCK
  324. value 153 at @ 0x9bba150 with ref count 1 in 4 INTBLOCK
  325. value 154 at @ 0x9bba144 with ref count 1 in 4 INTBLOCK
  326. value 155 at @ 0x9bba138 with ref count 1 in 4 INTBLOCK
  327. value 156 at @ 0x9bba12c with ref count 1 in 4 INTBLOCK
  328. value 157 at @ 0x9bba120 with ref count 1 in 4 INTBLOCK
  329. value 158 at @ 0x9bba114 with ref count 1 in 4 INTBLOCK
  330. value -5 at @ 0x9bba100 with ref count 1 in 5 INTBLOCK
  331. value -4 at @ 0x9bba0f4 with ref count 1 in 5 INTBLOCK
  332. value -3 at @ 0x9bba0e8 with ref count 1 in 5 INTBLOCK
  333. value -2 at @ 0x9bba0dc with ref count 1 in 5 INTBLOCK
  334. value -1 at @ 0x9bba0d0 with ref count 46 in 5 INTBLOCK
  335. value 0 at @ 0x9bba0c4 with ref count 315 in 5 INTBLOCK
  336. value 1 at @ 0x9bba0b8 with ref count 334 in 5 INTBLOCK
  337. value 2 at @ 0x9bba0ac with ref count 78 in 5 INTBLOCK
  338. value 3 at @ 0x9bba0a0 with ref count 23 in 5 INTBLOCK
  339. value 4 at @ 0x9bba094 with ref count 41 in 5 INTBLOCK
  340. value 5 at @ 0x9bba088 with ref count 17 in 5 INTBLOCK
  341. value 6 at @ 0x9bba07c with ref count 16 in 5 INTBLOCK
  342. value 7 at @ 0x9bba070 with ref count 17 in 5 INTBLOCK
  343. value 8 at @ 0x9bba064 with ref count 32 in 5 INTBLOCK
  344. value 9 at @ 0x9bba058 with ref count 14 in 5 INTBLOCK
  345. value 10 at @ 0x9bba04c with ref count 17 in 5 INTBLOCK
  346. value 11 at @ 0x9bba040 with ref count 16 in 5 INTBLOCK
  347. value 12 at @ 0x9bba034 with ref count 12 in 5 INTBLOCK
  348. value 13 at @ 0x9bba028 with ref count 11 in 5 INTBLOCK
  349. value 14 at @ 0x9bba01c with ref count 9 in 5 INTBLOCK
  350. value 15 at @ 0x9bba010 with ref count 11 in 5 INTBLOCK
  351. value 16 at @ 0x9bba004 with ref count 22 in 5 INTBLOCK
  352. value 17 at @ 0x9bb9ff8 with ref count 11 in 5 INTBLOCK
  353. value 18 at @ 0x9bb9fec with ref count 8 in 5 INTBLOCK
  354. value 19 at @ 0x9bb9fe0 with ref count 8 in 5 INTBLOCK
  355. value 20 at @ 0x9bb9fd4 with ref count 8 in 5 INTBLOCK
  356. value 21 at @ 0x9bb9fc8 with ref count 8 in 5 INTBLOCK
  357. value 22 at @ 0x9bb9fbc with ref count 8 in 5 INTBLOCK
  358. value 23 at @ 0x9bb9fb0 with ref count 9 in 5 INTBLOCK
  359. value 24 at @ 0x9bb9fa4 with ref count 8 in 5 INTBLOCK
  360. value 25 at @ 0x9bb9f98 with ref count 8 in 5 INTBLOCK
  361. value 26 at @ 0x9bb9f8c with ref count 8 in 5 INTBLOCK
  362. value 27 at @ 0x9bb9f80 with ref count 8 in 5 INTBLOCK
  363. value 28 at @ 0x9bb9f74 with ref count 8 in 5 INTBLOCK
  364. value 29 at @ 0x9bb9f68 with ref count 10 in 5 INTBLOCK
  365. value 30 at @ 0x9bb9f5c with ref count 9 in 5 INTBLOCK
  366. value 31 at @ 0x9bb9f50 with ref count 8 in 5 INTBLOCK
  367. value 32 at @ 0x9bb9f44 with ref count 14 in 5 INTBLOCK
  368. value 33 at @ 0x9bb9f38 with ref count 5 in 5 INTBLOCK
  369. value 34 at @ 0x9bb9f2c with ref count 7 in 5 INTBLOCK
  370. value 35 at @ 0x9bb9f20 with ref count 7 in 5 INTBLOCK
  371. value 36 at @ 0x9bb9f14 with ref count 5 in 5 INTBLOCK
  372. value 37 at @ 0x9bb9f08 with ref count 5 in 5 INTBLOCK
  373. value 38 at @ 0x9bb9efc with ref count 5 in 5 INTBLOCK
  374. value 39 at @ 0x9bb9ef0 with ref count 5 in 5 INTBLOCK
  375. value 40 at @ 0x9bb9ee4 with ref count 5 in 5 INTBLOCK
  376. value 41 at @ 0x9bb9ed8 with ref count 2 in 5 INTBLOCK
  377. value 42 at @ 0x9bb9ecc with ref count 5 in 5 INTBLOCK
  378. value 43 at @ 0x9bb9ec0 with ref count 5 in 5 INTBLOCK
  379. value 44 at @ 0x9bb9eb4 with ref count 5 in 5 INTBLOCK
  380. value 45 at @ 0x9bb9ea8 with ref count 5 in 5 INTBLOCK
  381. value 46 at @ 0x9bb9e9c with ref count 5 in 5 INTBLOCK
  382. value 47 at @ 0x9bb9e90 with ref count 5 in 5 INTBLOCK
  383. value 48 at @ 0x9bb9e84 with ref count 5 in 5 INTBLOCK
  384. value 49 at @ 0x9bb9e78 with ref count 5 in 5 INTBLOCK
  385. value 50 at @ 0x9bb9e6c with ref count 5 in 5 INTBLOCK
  386. value 51 at @ 0x9bb9e60 with ref count 5 in 5 INTBLOCK
  387. value 52 at @ 0x9bb9e54 with ref count 5 in 5 INTBLOCK
  388. value 53 at @ 0x9bb9e48 with ref count 6 in 5 INTBLOCK
  389. value 54 at @ 0x9bb9e3c with ref count 5 in 5 INTBLOCK
  390. value 55 at @ 0x9bb9e30 with ref count 5 in 5 INTBLOCK
  391. value 56 at @ 0x9bb9e24 with ref count 6 in 5 INTBLOCK
  392. value 57 at @ 0x9bb9e18 with ref count 5 in 5 INTBLOCK
  393. value 58 at @ 0x9bb9e0c with ref count 2 in 5 INTBLOCK
  394. value 59 at @ 0x9bb9e00 with ref count 5 in 5 INTBLOCK
  395. value 60 at @ 0x9bb9df4 with ref count 6 in 5 INTBLOCK
  396. value 61 at @ 0x9bb9de8 with ref count 5 in 5 INTBLOCK
  397. value 62 at @ 0x9bb9ddc with ref count 5 in 5 INTBLOCK
  398. value 63 at @ 0x9bb9dd0 with ref count 5 in 5 INTBLOCK
  399. value 64 at @ 0x9bb9dc4 with ref count 21 in 5 INTBLOCK
  400. value 65 at @ 0x9bb9db8 with ref count 10 in 5 INTBLOCK
  401. value 66 at @ 0x9bb9dac with ref count 8 in 5 INTBLOCK
  402. value 67 at @ 0x9bb9da0 with ref count 8 in 5 INTBLOCK
  403. value 68 at @ 0x9bb9d94 with ref count 8 in 5 INTBLOCK
  404. value 69 at @ 0x9bb9d88 with ref count 8 in 5 INTBLOCK
  405. value 70 at @ 0x9bb9d7c with ref count 8 in 5 INTBLOCK
  406. value 71 at @ 0x9bb9d70 with ref count 8 in 5 INTBLOCK
  407. value 72 at @ 0x9bb9d64 with ref count 9 in 5 INTBLOCK
  408. value 73 at @ 0x9bb9d58 with ref count 8 in 5 INTBLOCK
  409. value 74 at @ 0x9bb9d4c with ref count 8 in 5 INTBLOCK
  410. value 75 at @ 0x9bb9d40 with ref count 8 in 5 INTBLOCK
  411. value 76 at @ 0x9bb9d34 with ref count 8 in 5 INTBLOCK
  412. the free_list add is 0x9bdd5f0

从上述输出,我们可以清晰地看到小整数是缓存到INTBLOCK链表的尾部,同时在每一个INTBLOCK内部,两个小整数之间的地址之差刚好为12

同我们的结果相符,同时发现在INTBLOCk 1的开始部分有些没有被初始化,即free_list的部分,值都是不可信的

仔细的看一下源代码你会发现:

点击(此处)折叠或打开

  1. while (--q > p)
  2.         Py_TYPE(q) = (struct _typeobject *)(q-1);
你会发现在形成单向链表的时候,python使用了一个小技巧,即所有的对象都具有相同的开头部分,即ref_cnt,ob_type,

这样,暴漏出来的问题就是对小整形对象的类型检查无效了,那么python是如何进行类型检查的呢?

为此,我们修改类型检查的定义:

点击(此处)折叠或打开

  1. /* add by kinfinger */
  2.  423
  3.  424 #define CONVERT_TO_LONG(obj, lng)
  4.  425 if (PyInt_Check(obj)) {
  5.  426 printf("small int check or notn");
  6.  427 lng = PyInt_AS_LONG(obj);
  7.  428 }
  8.  429 else {
  9.  430 Py_INCREF(Py_NotImplemented);
  10.  431 return Py_NotImplemented;
  11.  432 }
发现当int为小整数是,是不进行类型检查的,只有当整数达到一定的范围时(2<<16-1)才进行真正的类型检查,这个没有在源代码发现,有知道的筒子可以告诉我。

至于大整数的缓存也是通过INTBLOCK来实现的,源码分析书中写的比较详细,在此不在介绍。

还有一个比较奇怪的现象是在对象进行初始化的时候,使用如下代码:

点击(此处)折叠或打开


  1. PyObject_INIT(v, &PyInt_Type);
  2. before  image:
  3. PyObject *
  4. PyObject_Init(PyObject *op, PyTypeObject *tp)
  5. {
  6.     if (op == NULL)
  7.         return PyErr_NoMemory();
  8.     /* Any changes should be reflected in PyObject_INIT (objimpl.h) */
  9.     Py_TYPE(op) = tp;
  10.     _Py_NewReference(op);
  11.     return op;
  12. }
  13. after  image: 
  14. /* this is edit by kinfinger */
     218 long int_call = 0;
     219 PyObject *
     220 PyObject_Init(PyObject *op, PyTypeObject *tp)
     221 {
     222     if (op == NULL)
     223         return PyErr_NoMemory();
     224     /* Any changes should be reflected in PyObject_INIT (objimpl.h) */
     225     printf("%s type \r\n",tp->tp_name);
     226     if (strcmp(tp->tp_name ,"int") == 0 ){ /* just for int */
     227             int_call += 1;
     228     printf("int call number is %ld \n",int_call);
     229     }
     230   /*        int_call += 1;
     231     printf("int call number is %ld \n",int_call);
     232     printf("in PyObject_init\r\n"); */
     233     Py_TYPE(op) = tp;
     234     _Py_NewReference(op);
     235     return op;
     236 }

貌似这是python所有对象的初始化部分,但是我们在进行确认的时候,发现程序打印出来的是类似如下消息:

buffer type 
PyCapsule type 
code type 
int call number is 617 
其中 int_call 的大小和我们的预期相符,每创建一个整数,该值加1,但是上面的信息确让人看不懂,留待以后研究

点击(此处)折叠或打开

  1. >>> a = 1
  2. >>> type(a)
  3. <type 'int'>
  4. >>> type(int)
  5. <type 'type'>
  6. >>> b =1
  7. >>> print hex(id(a)),hex(id(b))
  8. 0x160a2d8 0x160a2d8

ref: 

python源码分析

python manual

python源代码











阅读(3266) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~