{"id":5853,"date":"2019-11-24T22:11:06","date_gmt":"2019-11-25T06:11:06","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5853"},"modified":"2019-11-24T23:19:29","modified_gmt":"2019-11-25T07:19:29","slug":"python%e4%b8%ad%e7%9a%84%e6%95%b4%e5%9e%8b%e5%8d%a0%e5%a4%9a%e5%b0%91%e4%b8%aa%e5%ad%97%e8%8a%82%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/desgin\/python%e4%b8%ad%e7%9a%84%e6%95%b4%e5%9e%8b%e5%8d%a0%e5%a4%9a%e5%b0%91%e4%b8%aa%e5%ad%97%e8%8a%82%ef%bc%9f\/","title":{"rendered":"Python\u4e2d\u7684\u6574\u578b\u5360\u591a\u5c11\u4e2a\u5b57\u8282\uff1f"},"content":{"rendered":"\n<p>\u8bf4\u5230\u8ba1\u7b97\u673a\u4e2d\u7684\u6574\u578b\uff0c\u76f8\u4fe1\u5f88\u591a\u4eba\u90fd\u4f1a\u8054\u60f3\u523032\u4f4d\u6574\u578b\uff08\u6216\u8005int\uff09\uff0c\u662f\u7a0b\u5e8f\u5458\u65e5\u5e38\u751f\u6d3b\u4e2d\u7528\u7684\u6700\u591a\u7684\u4e00\u79cd\u7c7b\u578b\u300232\u4f4d\u6574\u578b\u987e\u540d\u601d\u4e49\uff0c\u5360\u752832\u4e2a\u4f4d\u4e5f\u5c31\u662f4\u4e2a\u5b57\u8282\uff0c\u53d6\u503c\u8303\u56f4\u22122,147,483,648~ 2,147,483,647 \u3002C\/C++\u4e2d\u662f4\u4e2a\u5b57\u8282\uff0cJava\u4e2d\u4e5f\u662f4\u4e2a\u5b57\u8282\uff0c\u4f46\u662fPython\u4e2d\u5462\uff1f<\/p>\n\n\n\n<p>\u6211\u4eec\u77e5\u9053Python\u4e2d\u4e5f\u6709int\u7c7b\uff0c\u800c\u4e14\u975e\u5e38\u597d\u7528\uff0c\u539f\u751f\u652f\u6301\u9ad8\u7cbe\u5ea6\u8ba1\u7b97\u3002\u4f46\u662fPython\u4e2d\u7684\u4e00\u4e2a\u6574\u578b\u5230\u5e95\u5360\u7528\u591a\u5c11\u5b57\u8282\u5462\uff1f\u6211\u76f8\u4fe1\u7edd\u5927\u591a\u6570\u7684Python\u7a0b\u5e8f\u5458\u4ece\u672a\u60f3\u8fc7\u8fd9\u4e00\u70b9\uff0c<strong>\u8d8a\u662f\u4e60\u4ee5\u4e3a\u5e38\u7684\u4e1c\u897f\u8d8a\u662f\u4e0d\u4f1a\u5728\u610f\u5b83\u7684\u5b58\u5728<\/strong>\u3002<\/p>\n\n\n\n<p>\u5728Python\u4e2d\uff0c\u5982\u679c\u60f3\u8981\u77e5\u9053\u4e00\u4e2a\u5bf9\u8c61\u6240\u5360\u7528\u7684\u5185\u5b58\u5927\u5c0f\uff0c\u53ea\u9700\u8981\u4f7f\u7528<code>sys.getsizeof<\/code>\u8fd9\u4e2aAPI\u5c31\u53ef\u4ee5\u4e86\u3002\u90a3\u5c31\u8ba9\u6211\u4eec\u6765\u8bd5\u4e00\u4e0b\u4e0d\u540c\u7684\u6574\u6570<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">print(sys.getsizeof(0))        # 24\nprint(sys.getsizeof(1))        # 28 \nprint(sys.getsizeof(2))        # 28\nprint(sys.getsizeof(2**15))    # 28\nprint(sys.getsizeof(2**30))    # 32\nprint(sys.getsizeof(2**128))   # 44<\/pre>\n\n\n\n<p>\u4ece\u4e0a\u9762\u7684\u5c0f\u5b9e\u9a8c\u53ef\u4ee5\u770b\u51fa\uff0c\u4e00\u4e2a\u6574\u578b\u6700\u5c11\u8981\u536024\u5b57\u8282(0)\uff0c1\u5f00\u59cb\u5c31\u8981\u5360\u752828\u4e2a\u5b57\u8282\uff0c\u5230\u4e862\u768430\u6b21\u65b9\u5f00\u59cb\u8981\u5360\u752832\u4e2a\u5b57\u8282\uff0c\u800c2\u7684128\u6b21\u65b9\u5219\u8981\u5360\u752844\u4e2a\u5b57\u8282\u3002\u6211\u4eec\u53ef\u4ee5\u5f97\u5230\u4e24\u70b9\u89c4\u5f8b\uff0c1. \u5b57\u8282\u6570\u968f\u7740\u6570\u5b57\u589e\u5927\u800c\u589e\u5927\u30022. \u6bcf\u6b21\u7684\u589e\u91cf\u662f4\u4e2a\u5b57\u8282\u3002  \u597d\u50cf\u81f3\u6b64\u5df2\u7ecf\u56de\u7b54\u4e86\u6211\u4eec\u7684\u9898\u76ee\u4e2d\u7684\u95ee\u9898\uff1aPython\u4e2d\u7684\u6574\u578b\u5360\u591a\u5c11\u4e2a\u5b57\u8282\uff1f\u7b54\u6848\u662f\uff1a\u53d8\u957f\u7684\uff08\u76f8\u5bf9\u4e8eint32\u7684\u5b9a\u957f\uff09\uff0c\u800c\u4e14\u6700\u5c1124\u4e2a\u5b57\u8282\u3002  <\/p>\n\n\n\n<p>\u4f60\u4ee5\u4e3a\u672c\u6587\u5230\u8fd9\u91cc\u5c31\u7ed3\u675f\u4e86\u5417\uff1f\u90a3\u4f60\u5c31\u56fe\u6837\u56fe\u68ee\u7834\u4e86\u3002\u4e00\u4e2a\u6574\u578b\u65702\uff0c\u5c45\u7136\u8981\u5360\u752828\u4e2a\u5b57\u8282\uff01\u8fd9\u5b8c\u5168\u4e86\u98a0\u8986\u4e86\u6211\u7684\u8ba4\u77e5\uff0c\u6211\u4e00\u5b9a\u641e\u6e05\u695a\u4e3a\u4ec0\u4e48\u3002 \u5728\u54e5\u4eec\u7684\u5e2e\u52a9\u4e0b\uff0c\u6211\u627e\u5230\u4e86Python\u7684\u6e90\u7801\u3002 <a href=\"https:\/\/github.com\/python\/cpython\">https:\/\/github.com\/python\/cpython<\/a>  <\/p>\n\n\n\n<p>Python\u7684\u5b98\u65b9\u5b9e\u73b0\u662fC\u8bed\u8a00\uff0c\u6240\u4ee5\u53ebcpython\u3002\u8fd9\u4e5f\u5c31\u610f\u5473\u7740\u53ea\u8981Python\u8fd8\u5728\uff0cC\u5c31\u4f1a\u4e0d\u6d88\u5931\u3002\u5176\u4ed6\u5b9e\u73b0\u8fd8\u6709jython(Java), IronPython (.Net), PyPy (Python)\u3002<\/p>\n\n\n\n<p>\u7b2c\u4e00\u4ef6\u4e8b\u60c5\u8981\u641e\u6e05\u695a\u7684\u662fPython\u4e2dint\u7c7b\u578b\u5728cpython\u7684\u540d\u5b57\u662f\u4ec0\u4e48\uff1f\u770b\u4e86\u534a\u5929\uff0c\u5728 <a href=\"https:\/\/github.com\/python\/cpython\/blob\/master\/Include\/longobject.h\">longobject.h<\/a> \u4e2d\u53d1\u73b0\u4e86\u4e00\u4e2a\u53eb  <code>PyLongObject<\/code> \u7684\u7ed3\u6784\u4f53\u3002\u7136\u800c\u5b83\u53ea\u662f\u4e00\u4e2a\u9a6c\u7532\uff0c\u662f <code>_longobject<\/code>\u7684\u522b\u540d\u3002\u5728<a href=\"https:\/\/github.com\/python\/cpython\/blob\/master\/Include\/longintrepr.h\">longintrepr.h<\/a>\u4e2d\u627e\u5230\u4e86 <code>_longobject  <\/code>\u7684\u5b9a\u4e49\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">struct _longobject {\n     PyObject_VAR_HEAD\n     digit ob_digit[1];\n };<\/pre>\n\n\n\n<p>\u5728\u6587\u4ef6\u7684\u5f00\u5934\u5c31\u770b\u5230\u4e86<code>typedef uint32_t digit;<\/code>\uff0c<code>digit<\/code>\u5c31\u662f<code>unit32_t<\/code>, \u6bcf\u4e2a\u5143\u7d20\u53604\u4e2a\u5b57\u8282\u3002\u4f46<code>PyObject_VAR_HEAD<\/code>\u53c8\u662f\u4ec0\u4e48\u9b3c\uff1f\u5728<a href=\"https:\/\/github.com\/python\/cpython\/blob\/master\/Include\/object.h\">object.h<\/a>\u4e2d\u53d1\u73b0\u4e86\u5b83\u662f\u4e2a\u5b8f\uff0c\u4e0a\u9762\u7684\u6ce8\u91ca\u5012\u662f\u633a\u6709\u610f\u601d\u7684\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/* PyObject_VAR_HEAD defines the initial segment of all variable-size\n container objects.  These end with a declaration of an array with 1\n element, but enough space is malloc'ed so that the array actually\n has room for ob_size elements.  Note that ob_size is an element count,\n not necessarily a byte count.\n *\/ \n define PyObject_VAR_HEAD      PyVarObject ob_base;<\/pre>\n\n\n\n<p>\u7b49\u4e00\u4e0b\uff0c<code>PyVarObject<\/code>\u53c8\u662f\u4ec0\u4e48\uff1f\u8fd8\u597d\u5b9a\u4e49\u5c31\u5728\u4e0b\u9762\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">typedef struct {\n     PyObject ob_base;\n     Py_ssize_t ob_size; \/* Number of items in variable part *\/\n } PyVarObject;<\/pre>\n\n\n\n<p>\u53c8\u4e00\u5c42\u5d4c\u5957\uff0c\u662f\u4e0d\u662f\u5df2\u7ecf\u6655\u4e86\uff0c\u7ee7\u7eed\u67e5\u770b<code>PyObject<\/code>\u7684\u5b9a\u4e49\uff0c\u8fd9\u6b21\u53cd\u800c\u5728\u4e0a\u9762\u4e86\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">typedef struct _object {\n     _PyObject_HEAD_EXTRA\n     Py_ssize_t ob_refcnt;\n     struct _typeobject *ob_type;\n } PyObject;<\/pre>\n\n\n\n<p>\u6709\u5b8c\u6ca1\u5b8c\u554a\uff1f<code>_PyObject_HEAD_EXTRA<\/code>\u53c8\u662f\u4ec0\u4e48\uff1f\u770b\u4e86\u4e00\u4e0b\u53d1\u73b0\u5b83\u53ea\u5728debug build\u4e2d\u6709\u5b9a\u4e49\uff0c\u8fd9\u91cc\u5c31\u4e0d\u5c55\u5f00\u4e86\u3002<code>Py_ssize_t<\/code>\u7b49\u4e8e<code>ssize_t<\/code>\u5982\u679c\u6709\u5b9a\u4e49\u7684\u8bdd\uff0c <code>ssize_t<\/code>\u572864\u4f4d\u7684\u673a\u5668\u4e0a\u5c31\u662f<code>long<\/code>\u3002<code>_typeobject<\/code>\u53c8\u662f\u4ec0\u4e48\uff1f\u611f\u89c9\u5e94\u8be5\u975e\u5e38\u5927\uff0c\u4e0d\u7136\u5c31\u4e0d\u4f1a\u7528\u6307\u9488\u4e86\u3002\u4e0d\u8fc7\u8bdd\u8bf4\u56de\u6765\uff0c\u65e2\u7136\u7528\u4e86\u6307\u9488\uff0c\u6211\u53c8\u4f55\u5fc5\u53bb\u5173\u5fc3\u5b83\u662f\u4ec0\u4e48\u5462\uff1f\u53cd\u6b63\u5c31\u662f8\u4e2a\u5b57\u8282\u800c\u5df2\uff0c\u6307\u5411\u4e00\u4e2a\u5185\u5b58\u5730\u5740\u3002\u81f3\u6b64\u771f\u76f8\u5927\u4e86\u4e00\u4e2a\u767d\uff0c\u5982\u679c\u6211\u4eec\u628astructs flatten,  <code>PyLongObject<\/code> \u5b9a\u4e49\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">struct PyLongObject {\n  long ob_refcnt;                \/\/ 8 bytes\n  struct _typeobject *ob_type;   \/\/ 8 bytes\n  long ob_size;                  \/\/ 8 bytes\n  unsigned int ob_digit[1];      \/\/ 4 bytes * abs(ob_size)\n};<\/pre>\n\n\n\n<p><code>ob_refcnt<\/code>\u5f15\u7528\u8ba1\u6570 8\u4e2a\u5b57\u8282\uff0c<code>ob_type<\/code>\u7c7b\u578b\u4fe1\u606f 8\u4e2a\u5b57\u8282\uff08\u6307\u9488\uff09\uff0c<code>ob_size<\/code>\u53d8\u957f\u90e8\u5206\u5143\u7d20\u7684\u4e2a\u6570\uff0c8\u4e2a\u5b57\u8282\u3002<code>ob_digit<\/code>\u53d8\u957f\u7684\u6570\u636e\u90e8\u5206\uff0c\u5b57\u8282\u6570\u4e3a4*abs(<code>ob_size<\/code>)\uff0c<code>ob_size<\/code>\u53ef\u4ee5\u4e3a0\uff0c\u6240\u4ee5\u6700\u5c118+8+8=24\u5b57\u8282\uff0c\u6bcf\u6b21\u589e\u91cf\u90fd\u662f4 (<code>unsigned int<\/code>) \u7684\u500d\u6570\u3002\u8fd9\u548c\u6211\u4eec\u4e4b\u524d\u89c2\u5bdf\u5230\u7684\u5b9e\u9a8c\u7ed3\u679c\u543b\u5408\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0a\u90fd\u662f\u57fa\u4e8e64\u4f4d\u7684Python\uff0c\u5bf9\u4e8e32\u4f4d\u7684\u7248\u672c\uff0c\u5b9a\u4e49\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">struct PyLongObject {\n  int ob_refcnt;                \/\/ 4 bytes\n  struct _typeobject *ob_type;  \/\/ 4 bytes\n  int ob_size;                  \/\/ 4 bytes\n  unsigned short ob_digit[1];   \/\/ 2 bytes * abs(ob_size)\n};<\/pre>\n\n\n\n<p>32\u4f4d\u5c31\u8981\u6bd464\u4f4d\u5c0f\u5f88\u591a\u4e86\uff0c\u6700\u5c1112\u4e2a\u5b57\u8282\uff0c\u589e\u91cf\u4e3a2\u4e2a\u5b57\u8282\u3002<\/p>\n\n\n\n<p>\u597d\u4e86\uff0c\u4eca\u5929\u5c31\u5199\u5230\u8fd9\u91cc\u3002\u76f8\u4fe1\u4f60\u5bf9\u6574\u578b\u6570\u6216\u8005Python\u6709\u4e86\u4e00\u4e2a\u65b0\u7684\u8ba4\u8bc6\u3002\u4e0b\u9762\u4e00\u7bc7\u6211\u4eec\u4f1a\u5c06\u4f1a\u4ecb\u7ecd\u6574\u578b\u6570\u5728Python\u4e2d\u7684\u8868\u793a\u548c\u8ba1\u7b97\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8bf4\u5230\u8ba1\u7b97\u673a\u4e2d\u7684\u6574\u578b\uff0c\u76f8\u4fe1\u5f88\u591a\u4eba\u90fd\u4f1a\u8054\u60f3\u523032\u4f4d\u6574\u578b\uff08\u6216\u8005int\uff09\uff0c\u662f\u7a0b\u5e8f\u5458\u65e5\u5e38\u751f\u6d3b\u4e2d\u7528\u7684\u6700\u591a\u7684\u4e00\u79cd\u7c7b\u578b\u300232\u4f4d\u6574\u578b\u987e\u540d\u601d\u4e49\uff0c\u5360\u752832\u4e2a\u4f4d\u4e5f\u5c31\u662f4\u4e2a\u5b57\u8282\uff0c\u53d6\u503c\u8303\u56f4\u22122,147,483,648~ 2,147,483,647 \u3002C\/C++\u4e2d\u662f4\u4e2a\u5b57\u8282\uff0cJava\u4e2d\u4e5f\u662f4\u4e2a\u5b57\u8282\uff0c\u4f46\u662fPython\u4e2d\u5462\uff1f \u6211\u4eec\u77e5\u9053Python\u4e2d\u4e5f\u6709int\u7c7b\uff0c\u800c\u4e14\u975e\u5e38\u597d\u7528\uff0c\u539f\u751f\u652f\u6301\u9ad8\u7cbe\u5ea6\u8ba1\u7b97\u3002\u4f46\u662fPython\u4e2d\u7684\u4e00\u4e2a\u6574\u578b\u5230\u5e95\u5360\u7528\u591a\u5c11\u5b57\u8282\u5462\uff1f\u6211\u76f8\u4fe1\u7edd\u5927\u591a\u6570\u7684Python\u7a0b\u5e8f\u5458\u4ece\u672a\u60f3\u8fc7\u8fd9\u4e00\u70b9\uff0c\u8d8a\u662f\u4e60\u4ee5\u4e3a\u5e38\u7684\u4e1c\u897f\u8d8a\u662f\u4e0d\u4f1a\u5728\u610f\u5b83\u7684\u5b58\u5728\u3002 \u5728Python\u4e2d\uff0c\u5982\u679c\u60f3\u8981\u77e5\u9053\u4e00\u4e2a\u5bf9\u8c61\u6240\u5360\u7528\u7684\u5185\u5b58\u5927\u5c0f\uff0c\u53ea\u9700\u8981\u4f7f\u7528sys.getsizeof\u8fd9\u4e2aAPI\u5c31\u53ef\u4ee5\u4e86\u3002\u90a3\u5c31\u8ba9\u6211\u4eec\u6765\u8bd5\u4e00\u4e0b\u4e0d\u540c\u7684\u6574\u6570 print(sys.getsizeof(0)) # 24 print(sys.getsizeof(1)) # 28 print(sys.getsizeof(2)) # 28 print(sys.getsizeof(2**15)) # 28 print(sys.getsizeof(2**30)) # 32 print(sys.getsizeof(2**128)) # 44 \u4ece\u4e0a\u9762\u7684\u5c0f\u5b9e\u9a8c\u53ef\u4ee5\u770b\u51fa\uff0c\u4e00\u4e2a\u6574\u578b\u6700\u5c11\u8981\u536024\u5b57\u8282(0)\uff0c1\u5f00\u59cb\u5c31\u8981\u5360\u752828\u4e2a\u5b57\u8282\uff0c\u5230\u4e862\u768430\u6b21\u65b9\u5f00\u59cb\u8981\u5360\u752832\u4e2a\u5b57\u8282\uff0c\u800c2\u7684128\u6b21\u65b9\u5219\u8981\u5360\u752844\u4e2a\u5b57\u8282\u3002\u6211\u4eec\u53ef\u4ee5\u5f97\u5230\u4e24\u70b9\u89c4\u5f8b\uff0c1. \u5b57\u8282\u6570\u968f\u7740\u6570\u5b57\u589e\u5927\u800c\u589e\u5927\u30022.&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[340],"tags":[517,518],"class_list":["post-5853","post","type-post","status-publish","format-standard","hentry","category-desgin","tag-python","tag-source-code","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5853","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=5853"}],"version-history":[{"count":9,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5853\/revisions"}],"predecessor-version":[{"id":5866,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5853\/revisions\/5866"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5853"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5853"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5853"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}