{"id":8872,"date":"2023-08-29T14:42:07","date_gmt":"2023-08-29T06:42:07","guid":{"rendered":"\/?p=8872"},"modified":"2023-08-29T14:43:10","modified_gmt":"2023-08-29T06:43:10","slug":"python%e7%bc%96%e8%af%91%e5%99%a8%ef%bc%88%e4%b8%89%ef%bc%89%ef%bc%9a%e8%bf%90%e8%a1%8c%e6%97%b6%e6%9c%ba%e5%88%b6","status":"publish","type":"post","link":"\/?p=8872","title":{"rendered":"Python\u7f16\u8bd1\u5668\uff08\u4e09\uff09\uff1a\u8fd0\u884c\u65f6\u673a\u5236"},"content":{"rendered":"<p>\u5728\u524d\u9762\u4e24\u8bb2\u4e2d\uff0c\u6211\u4eec\u5df2\u7ecf\u5206\u6790\u4e86 Python \u4ece\u5f00\u59cb\u7f16\u8bd1\u5230\u751f\u6210\u5b57\u8282\u7801\u7684\u673a\u5236\u3002\u4f46\u662f\uff0c\u6211\u4eec\u5bf9 Python \u53ea\u662f\u4e86\u89e3\u4e86\u4e00\u534a\uff0c\u8fd8\u6709\u5f88\u591a\u95ee\u9898\u9700\u8981\u89e3\u7b54\u3002\u6bd4\u5982\uff1a<strong>Python \u5b57\u8282\u7801\u662f\u5982\u4f55\u8fd0\u884c\u7684\u5462\uff1f\u5b83\u662f\u5982\u4f55\u7ba1\u7406\u7a0b\u5e8f\u6240\u7528\u5230\u7684\u6570\u636e\u7684\uff1f\u5b83\u7684\u7c7b\u578b\u4f53\u7cfb\u662f\u5982\u4f55\u8bbe\u8ba1\u7684\uff0c\u6709\u4ec0\u4e48\u7279\u70b9\uff1f<\/strong>\u7b49\u7b49\u3002<\/p>\n<p>\u6240\u4ee5\u4eca\u5929\u8fd9\u4e00\u8bb2\uff0c\u6211\u4eec\u5c31\u6765\u8ba8\u8bba\u4e00\u4e0b Python \u7684\u8fd0\u884c\u65f6\u673a\u5236\u3002\u5176\u4e2d\u7684<strong>\u6838\u5fc3\uff0c\u662f Python \u5bf9\u8c61\u673a\u5236\u7684\u8bbe\u8ba1<\/strong>\u3002<\/p>\n<p>\u6211\u4eec\u5148\u6765\u7814\u7a76\u4e00\u4e0b\u5b57\u8282\u7801\u7684\u8fd0\u884c\u673a\u5236\u3002\u4f60\u4f1a\u53d1\u73b0\uff0c\u5b83\u8ddf Python \u7684\u5bf9\u8c61\u673a\u5236\u5bc6\u5207\u76f8\u5173\u3002<\/p>\n<h2>\u7406\u89e3\u5b57\u8282\u7801\u7684\u6267\u884c\u8fc7\u7a0b<\/h2>\n<p>\u6211\u4eec\u7528 GDB \u8ddf\u8e2a\u6267\u884c\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u7a0b\u5e8f\uff0c\u5b83\u53ea\u6709\u4e00\u884c\uff1a\u201ca=1\u201d\u3002<\/p>\n<p>\u8fd9\u884c\u4ee3\u7801\u5bf9\u5e94\u7684\u5b57\u8282\u7801\u5982\u4e0b\u3002\u5176\u4e2d\uff0c\u524d\u4e24\u884c\u6307\u4ee4\u5b9e\u73b0\u4e86\u201ca=1\u201d\u7684\u529f\u80fd\uff08\u540e\u4e24\u884c\u662f\u6839\u636e Python \u7684\u89c4\u5b9a\uff0c\u5728\u6267\u884c\u5b8c\u4e00\u4e2a\u6a21\u5757\u4e4b\u540e\uff0c\u7f3a\u7701\u8fd4\u56de\u4e00\u4e2a None \u503c\uff09\u3002<\/p>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20230829143211795.png\" alt=\"image-20230829143211795\" \/><\/p>\n<p>\u4f60\u9700\u8981\u5728 <strong>_PyEval_EvalFrameDefault() \u51fd\u6570<\/strong>\u8fd9\u91cc\u8bbe\u7f6e\u4e00\u4e2a\u65ad\u70b9\uff0c\u5728\u8fd9\u91cc\u5b9e\u9645\u89e3\u91ca\u6307\u4ee4\u5e76\u6267\u884c\u3002<\/p>\n<p><strong>\u9996\u5148\u662f\u6267\u884c\u7b2c\u4e00\u884c\u6307\u4ee4\uff0cLOAD_CONST\u3002<\/strong><\/p>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20230829143225307.png\" alt=\"image-20230829143225307\" \/><\/p>\n<p>\u4f60\u4f1a\u770b\u5230\uff0c\u89e3\u91ca\u5668\u505a\u4e86\u4e09\u4ef6\u4e8b\u60c5\uff1a<\/p>\n<p>\u4ece\u5e38\u6570\u8868\u91cc\u53d6\u51fa 0 \u53f7\u5e38\u6570\u3002\u4f60\u77e5\u9053\uff0c\u7f16\u8bd1\u5b8c\u6bd5\u4ee5\u540e\u4f1a\u5f62\u6210 PyCodeObject\uff0c\u800c\u5728\u8fd9\u4e2a\u5bf9\u8c61\u91cc\u4f1a\u8bb0\u5f55\u6240\u6709\u7684\u5e38\u91cf\u3001\u7b26\u53f7\u540d\u79f0\u3001\u672c\u5730\u53d8\u91cf\u7b49\u4fe1\u606f\u3002\u5e38\u91cf 1 \u5c31\u662f\u4ece\u5b83\u7684\u5e38\u91cf\u8868\u4e2d\u53d6\u51fa\u6765\u7684\u3002<\/p>\n<p>\u628a\u5bf9\u8c61\u5f15\u7528\u503c\u52a0 1\u3002\u5bf9\u8c61\u5f15\u7528\u8ddf\u5783\u573e\u6536\u96c6\u673a\u5236\u76f8\u5173\u3002<\/p>\n<p>\u628a\u8fd9\u4e2a\u5e38\u6570\u5bf9\u8c61\u5165\u6808\u3002<\/p>\n<p>\u4ece\u8fd9\u7b2c\u4e00\u884c\u6307\u4ee4\u7684\u6267\u884c\u8fc7\u7a0b\uff0c\u4f60\u80fd\u5f97\u5230\u4ec0\u4e48\u4fe1\u606f\u5462\uff1f<\/p>\n<p>\u7b2c\u4e00\u4e2a\u4fe1\u606f\uff0c\u5e38\u91cf 1 \u5728 Python \u5185\u90e8\uff0c\u5b83\u662f\u4e00\u4e2a\u5bf9\u8c61\u3002\u4f60\u53ef\u4ee5\u5728 GDB \u91cc\u663e\u793a\u8fd9\u4e2a\u5bf9\u8c61\u7684\u4fe1\u606f\uff1a\u8be5\u5bf9\u8c61\u7684\u7c7b\u578b\u662f PyLong_Type \u578b\uff0c\u8fd9\u662f Python \u7684\u6574\u578b\u5728\u5185\u90e8\u7684\u5b9e\u73b0\u3002<\/p>\n<p>\u53e6\u5916\uff0c\u8be5\u5bf9\u8c61\u7684\u5f15\u7528\u6570\u662f 126 \u4e2a\uff0c\u8bf4\u660e\u8fd9\u4e2a\u5e38\u91cf\u5bf9\u8c61\u5176\u5b9e\u662f\u88ab\u5171\u4eab\u7684\uff0cLOAD_CONST \u6307\u4ee4\u4f1a\u8ba9\u5b83\u7684\u5f15\u7528\u6570\u52a0 1\u3002\u6211\u4eec\u7528\u7684\u5e38\u6570\u662f 1\uff0c\u8fd9\u4e2a\u503c\u5728 Python \u5185\u90e8\u4e5f\u662f\u4f1a\u7ecf\u5e38\u88ab\u7528\u5230\uff0c\u6240\u4ee5\u5f15\u7528\u6570\u4f1a\u8fd9\u4e48\u9ad8\u3002\u4f60\u53ef\u4ee5\u8bd5\u7740\u9009\u4e2a\u4e0d\u90a3\u4e48\u5e38\u89c1\u7684\u5e38\u6570\uff0c\u770b\u770b\u5b83\u7684\u5f15\u7528\u6570\u662f\u591a\u5c11\uff0c\u90fd\u662f\u5728\u54ea\u91cc\u88ab\u5f15\u7528\u7684\u3002<\/p>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20230829143240344.png\" alt=\"image-20230829143240344\" \/><\/p>\n<p>\u8fdb\u4e00\u6b65\uff0c\u6211\u4eec\u4f1a\u53d1\u73b0\uff0c\u5f80\u6808\u91cc\u653e\u7684\u6570\u636e\uff0c\u5176\u5b9e\u662f\u4e2a\u5bf9\u8c61\u6307\u9488\uff0c\u800c\u4e0d\u50cf Java \u7684\u6808\u673a\u90a3\u6837\uff0c\u662f\u653e\u4e86\u4e2a\u6574\u6570\u3002<\/p>\n<p>\u603b\u7ed3\u4e0a\u8ff0\u4fe1\u606f\uff0c\u6211\u5176\u5b9e\u53ef\u4ee5\u544a\u8bc9\u4f60\u4e00\u4e2a\u7ed3\u8bba\uff1a<strong>\u5728 Python \u91cc\uff0c\u7a0b\u5e8f\u4e2d\u7684\u4efb\u4f55\u7b26\u53f7\u90fd\u662f\u5bf9\u8c61\uff0c\u5305\u62ec\u6574\u6570\u3001\u6d6e\u70b9\u6570\u8fd9\u4e9b\u57fa\u7840\u6570\u636e\uff0c\u6216\u8005\u662f\u81ea\u5b9a\u4e49\u7684\u7c7b\uff0c\u6216\u8005\u662f\u51fd\u6570\uff0c\u5b83\u4eec\u90fd\u662f\u5bf9\u8c61\u3002<\/strong>\u5728\u6808\u673a\u91cc\u5904\u7406\u7684\uff0c\u662f\u8fd9\u4e9b\u5bf9\u8c61\u7684\u5f15\u7528\u3002<\/p>\n<p><strong>\u6211\u4eec\u518d\u7ee7\u7eed\u5f80\u4e0b\u5206\u6790\u4e00\u6761\u6307\u4ee4\uff0c\u4e5f\u5c31\u662f STORE_NAME \u6307\u4ee4<\/strong>\uff0c\u6765\u52a0\u6df1\u4e00\u4e0b\u5bf9 Python \u8fd0\u884c\u673a\u5236\u7684\u7406\u89e3\u3002<\/p>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20230829143252971.png\" alt=\"image-20230829143252971\" \/><\/p>\n<p>\u6267\u884c STORE_NAME \u6307\u4ee4\u65f6\uff0c\u89e3\u91ca\u5668\u505a\u4e86 5 \u4ef6\u4e8b\u60c5\uff1a<\/p>\n<p>\u6839\u636e\u6307\u4ee4\u7684\u53c2\u6570\uff0c\u4ece\u540d\u79f0\u8868\u91cc\u53d6\u51fa\u53d8\u91cf\u540d\u79f0\u3002\u8fd9\u4e2a\u540d\u79f0\u8868\u4e5f\u662f\u6765\u81ea\u4e8e PyCodeObject\u3002\u524d\u9762\u6211\u521a\u8bf4\u8fc7\u4e86\uff0cPython \u7a0b\u5e8f\u4e2d\u7684\u4e00\u5207\u90fd\u662f\u5bf9\u8c61\uff0c\u90a3\u4e48 name \u4e5f\u662f\u5bf9\u8c61\u3002\u4f60\u53ef\u4ee5\u67e5\u770b\u5b83\u7684\u7c7b\u578b\uff0c\u662f PyUnicode_Type\uff0c\u4e5f\u5c31\u662f Unicode \u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<p>\u4ece\u6808\u9876\u5f39\u51fa\u4e0a\u4e00\u6b65\u5b58\u8fdb\u53bb\u7684\u5e38\u91cf\u5bf9\u8c61\u3002<\/p>\n<p>\u83b7\u53d6\u4fdd\u5b58\u4e86\u6240\u6709\u672c\u5730\u53d8\u91cf\u7684\u5b57\u5178\uff0c\u8fd9\u4e5f\u662f\u6765\u81ea PyCodeObject\u3002<\/p>\n<p>\u5728\u5b57\u5178\u91cc\uff0c\u8bbe\u7f6e a \u7684\u503c\u4e3a\u8be5\u5e38\u91cf\u3002\u5982\u679c\u4f60\u6df1\u5165\u8ddf\u8e2a\u5176\u6267\u884c\u8fc7\u7a0b\uff0c\u4f60\u4f1a\u53d1\u73b0\u5728\u5b58\u5165\u5b57\u5178\u7684\u65f6\u5019\uff0cname \u5bf9\u8c61\u548c v \u5bf9\u8c61\u7684\u5f15\u7528\u90fd\u4f1a\u52a0 1\u3002\u8fd9\u4e5f\u662f\u53ef\u4ee5\u7406\u89e3\u7684\uff0c\u56e0\u4e3a\u5b83\u4eec\u4e00\u4e2a\u4f5c\u4e3a key\uff0c\u4e00\u4e2a\u4f5c\u4e3a value\uff0c\u90fd\u8981\u88ab\u5b57\u5178\u6240\u5f15\u7528\u3002<\/p>\n<p>\u51cf\u5c11\u5e38\u91cf\u5bf9\u8c61\u7684\u5f15\u7528\u8ba1\u6570\u3002\u610f\u601d\u662f\u6808\u673a\u672c\u8eab\u4e0d\u518d\u5f15\u7528\u8be5\u5e38\u91cf\u3002<\/p>\n<p>\u597d\u4e86\uff0c\u901a\u8fc7\u8be6\u7ec6\u89e3\u8bfb\u8fd9\u4e24\u6761\u6307\u4ee4\u7684\u6267\u884c\u8fc7\u7a0b\uff0c\u6211\u76f8\u4fe1\u4f60\u5bf9 Python \u7684\u8fd0\u884c\u673a\u5236\u6478\u5230\u4e00\u70b9\u5934\u7eea\u4e86\uff0c\u4f46\u53ef\u80fd\u8fd8\u662f\u4f1a\u63d0\u51fa\u5f88\u591a\u95ee\u9898\u6765\uff0c\u6bd4\u5982\u8bf4\uff1a<\/p>\n<p><strong>\u65e2\u7136\u6808\u91cc\u7684\u64cd\u4f5c\u6570\u90fd\u662f\u5bf9\u8c61\u6307\u9488\uff0c\u90a3\u4e48\u5982\u4f55\u505a\u52a0\u51cf\u4e58\u9664\u7b49\u7b97\u672f\u8fd0\u7b97\uff1f<\/strong><\/p>\n<p><strong>\u5982\u679c\u51fd\u6570\u4e5f\u662f\u5bf9\u8c61\uff0c\u90a3\u4e48\u6267\u884c\u51fd\u6570\u7684\u8fc7\u7a0b\u53c8\u662f\u600e\u6837\u7684\uff1f<\/strong><\/p>\n<p>\u2026\u2026<\/p>\n<p>\u522b\u7740\u6025\uff0c\u6211\u5728\u540e\u9762\u4f1a\u5e26\u4f60\u63a2\u7a76\u6e05\u695a\u8fd9\u4e9b\u95ee\u9898\u3002\u4e0d\u8fc7\u5728\u6b64\u4e4b\u524d\uff0c\u6211\u4eec\u6709\u5fc5\u8981\u5148\u52a0\u6df1\u4e00\u4e0b\u5bf9 Python \u5bf9\u8c61\u7684\u4e86\u89e3\u3002<\/p>\n<h2>Python \u5bf9\u8c61\u7684\u8bbe\u8ba1<\/h2>\n<p>Python \u7684\u5bf9\u8c61\u5b9a\u4e49\u5728 object.h \u4e2d\u3002\u9605\u8bfb\u6587\u4ef6\u5934\u90e8\u7684\u6ce8\u91ca\u548c\u5bf9\u5404\u7c7b\u6570\u636e\u7ed3\u6784\u7684\u5b9a\u4e49\uff0c\u4f60\u5c31\u53ef\u4ee5\u7406\u89e3 Python \u5bf9\u8c61\u7684\u8bbe\u8ba1\u601d\u8def\u3002<\/p>\n<p>\u9996\u5148\u662f PyObject \u548c PyVarObject \u4e24\u4e2a\u57fa\u7840\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5b83\u4eec\u5206\u522b\u8868\u793a\u5b9a\u957f\u7684\u6570\u636e\u548c\u53d8\u957f\u7684\u6570\u636e\u3002<\/p>\n<pre><code class=\"language-c\">typedef struct _object {          \/\/\u5b9a\u957f\u5bf9\u8c61\n    Py_ssize_t ob_refcnt;         \/\/\u5bf9\u8c61\u5f15\u7528\u8ba1\u6570\n    struct _typeobject *ob_type;  \/\/\u5bf9\u8c61\u7c7b\u578b\n} PyObject;\ntypedef struct {         \/\/\u53d8\u957f\u5bf9\u8c61\n    PyObject ob_base;             \n    Py_ssize_t ob_size;  \/\/\u53d8\u957f\u90e8\u5206\u7684\u9879\u76ee\u6570\u91cf\uff0c\u5728\u7533\u8bf7\u5185\u5b58\u65f6\u6709\u786e\u5b9a\u7684\u503c\uff0c\u4e0d\u518d\u53d8\n} PyVarObject;<\/code><\/pre>\n<p>PyObject \u662f\u6700\u57fa\u7840\u7684\u7ed3\u6784\uff0c\u6240\u6709\u7684\u5bf9\u8c61\u5728 Python \u5185\u90e8\u90fd\u8868\u793a\u4e3a\u4e00\u4e2a PyObject \u6307\u9488\u3002\u5b83\u91cc\u9762\u53ea\u5305\u542b\u4e24\u4e2a\u6210\u5458\uff1a\u5bf9\u8c61\u5f15\u7528\u8ba1\u6570\uff08ob_refcnt\uff09\u548c\u5bf9\u8c61\u7c7b\u578b\uff08ob_type\uff09\uff0c\u4f60\u5728\u7528 GDB \u8ddf\u8e2a\u6267\u884c\u65f6\u4e5f\u89c1\u8fc7\u5b83\u4eec\u3002\u53ef\u80fd\u4f60\u4f1a\u95ee\uff0c<strong>\u4e3a\u4ec0\u4e48\u53ea\u6709\u8fd9\u4e24\u4e2a\u6210\u5458\u5462\uff1f\u5bf9\u8c61\u7684\u6570\u636e\uff08\u6bd4\u5982\u4e00\u4e2a\u6574\u6570\uff09\u4fdd\u5b58\u5728\u54ea\u91cc\uff1f<\/strong><\/p>\n<p>\u5b9e\u9645\u4e0a\uff0c\u4efb\u4f55\u5bf9\u8c61\u90fd\u4f1a\u5728\u4e00\u5f00\u5934\u5305\u542b PyObject\uff0c\u5176\u4ed6\u6570\u636e\u90fd\u8ddf\u5728 PyObject \u7684\u540e\u9762\u3002\u6bd4\u5982\u8bf4\uff0cPython3 \u7684\u6574\u6570\u7684\u8bbe\u8ba1\u662f\u4e00\u4e2a\u53d8\u957f\u5bf9\u8c61\uff0c\u4f1a\u7528\u4e00\u5230\u591a\u4e2a 32 \u4f4d\u7684\u6bb5\uff0c\u6765\u8868\u793a\u4efb\u610f\u4f4d\u6570\u7684\u6574\u6570\uff1a<\/p>\n<pre><code class=\"language-c\">#define PyObject_VAR_HEAD      PyVarObject ob_base;\nstruct _longobject {\n    PyObject_VAR_HEAD       \/\/PyVarObject\n    digit ob_digit[1];      \/\/\u6570\u5b57\u6bb5\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\n};\ntypedef struct _longobject PyLongObject;    \/\/\u6574\u578b<\/code><\/pre>\n<p>\u5b83\u5728\u5185\u5b58\u4e2d\u7684\u5e03\u5c40\u662f\u8fd9\u6837\u7684\uff1a<\/p>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20230829143342706.png\" alt=\"image-20230829143342706\" \/><\/p>\n<p>\u6240\u4ee5\u4f60\u4f1a\u770b\u51fa\uff0c<strong>PyObject***<\/strong>\u3001<strong><strong>PyVarObject*<\/strong><\/strong>\u548c<strong><strong>PyLongObject*<\/strong><\/strong>\u6307\u5411\u7684\u5185\u5b58\u5730\u5740\u662f\u76f8\u540c\u7684*<em>\u3002\u4f60\u53ef\u4ee5\u6839\u636e ob_type \u7684\u7c7b\u578b\uff0c\u628aPyObject<\/em>\u5f3a\u5236\u8f6c\u6362\u6210PyLongObject*\u3002<\/p>\n<p>\u5b9e\u9645\u4e0a\uff0c\u50cf C++ \u8fd9\u6837\u7684\u9762\u5411\u5bf9\u8c61\u8bed\u8a00\u7684\u5185\u5b58\u5e03\u5c40\u4e5f\u662f\u5982\u6b64\uff0c\u7236\u7c7b\u7684\u6210\u5458\u53d8\u91cf\u5728\u6700\u524d\u9762\uff0c\u5b50\u7c7b\u7684\u6210\u5458\u53d8\u91cf\u5728\u540e\u9762\uff0c\u7236\u7c7b\u548c\u5b50\u7c7b\u7684\u6307\u9488\u4e4b\u95f4\u53ef\u4ee5\u5f3a\u5236\u8f6c\u6362\u3002\u61c2\u5f97\u4e86\u8fd9\u4e2a\u539f\u7406\uff0c\u6211\u4eec\u7528 C \u8bed\u8a00\u7167\u6837\u53ef\u4ee5\u6a21\u62df\u51fa\u9762\u5411\u5bf9\u8c61\u7684\u7ee7\u627f\u7ed3\u6784\u51fa\u6765\u3002<\/p>\n<p>\u4f60\u53ef\u4ee5\u6ce8\u610f\u5230\uff0c\u6211\u5728\u56fe 1 \u4e2d\u6807\u51fa\u4e86\u6bcf\u4e2a\u5b57\u6bb5\u6240\u5360\u5185\u5b58\u7684\u5927\u5c0f\uff0c\u603b\u5171\u662f 28 \u4e2a\u5b57\u8282\uff08\u8fd9\u662f 64 \u4f4d macOS \u4e0b\u7684\u6570\u503c\uff0c\u5982\u679c\u662f\u53e6\u5916\u7684\u73af\u5883\uff0c\u6bd4\u5982 32 \u4f4d\u73af\u5883\uff0c\u6570\u503c\u4f1a\u6709\u6240\u4e0d\u540c\uff09\u3002<\/p>\n<p>\u4f60\u53ef\u4ee5\u7528 sys.getsizeof() \u51fd\u6570\uff0c\u6765\u6d4b\u91cf\u5bf9\u8c61\u5360\u636e\u7684\u5185\u5b58\u7a7a\u95f4\u3002<\/p>\n<pre><code>>&gt;&gt; a = 10\n>&gt;&gt; import sys\n>&gt;&gt; sys.getsizeof(a)\n28       #ob_size = 1\n>&gt;&gt; a = 1234567890\n>&gt;&gt; sys.getsizeof(a)\n32       #ob_size = 2\uff0c\u652f\u6301\u66f4\u5927\u7684\u6574\u6570<\/code><\/pre>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u603b\u7ed3\u4e00\u4e0b Python \u5bf9\u8c61\u8bbe\u8ba1\u7684\u4e09\u4e2a\u7279\u70b9\uff1a<\/p>\n<p><strong>1. \u57fa\u4e8e\u5806<\/strong><\/p>\n<p>Python \u5bf9\u8c61\u5168\u90e8\u90fd\u662f\u5728\u5806\u91cc\u7533\u8bf7\u7684\uff0c\u6ca1\u6709\u9759\u6001\u7533\u8bf7\u548c\u5728\u6808\u91cc\u7533\u8bf7\u7684\u3002\u8fd9\u8ddf C\u3001C++ \u548c Java \u8fd9\u6837\u7684\u9759\u6001\u7c7b\u578b\u7684\u8bed\u8a00\u5f88\u4e0d\u4e00\u6837\u3002<\/p>\n<p>C \u7684\u7ed3\u6784\u4f53\u548c C++ \u7684\u5bf9\u8c61\u90fd\u65e2\u53ef\u4ee5\u5728\u6808\u91cc\uff0c\u4e5f\u53ef\u4ee5\u5728\u5806\u91cc\uff1bJava \u4e5f\u662f\u4e00\u6837\uff0c\u9664\u4e86\u539f\u751f\u6570\u636e\u7c7b\u578b\u53ef\u4ee5\u5728\u6808\u91cc\u7533\u8bf7\uff0c\u672a\u9003\u9038\u7684 Java \u5bf9\u8c61\u7684\u5185\u5b58\u4e5f\u53ef\u4ee5\u5728\u6808\u91cc\u7ba1\u7406\uff0c\u6211\u4eec\u5728\u8bb2Java \u7684 JIT \u7f16\u8bd1\u5668\u7684\u65f6\u5019\u5df2\u7ecf\u8bb2\u8fc7\u4e86\u3002<\/p>\n<p><strong>2. \u57fa\u4e8e\u5f15\u7528\u8ba1\u6570\u7684\u5783\u573e\u6536\u96c6\u673a\u5236<\/strong><\/p>\n<p>\u6bcf\u4e2a Python \u5bf9\u8c61\u4f1a\u4fdd\u5b58\u4e00\u4e2a\u5f15\u7528\u8ba1\u6570\u3002\u4e5f\u5c31\u662f\u8bf4\uff0cPython \u7684\u5783\u573e\u6536\u96c6\u673a\u5236\u662f\u57fa\u4e8e\u5f15\u7528\u8ba1\u6570\u7684\u3002<\/p>\n<p>\u5b83\u7684\u4f18\u70b9\u662f\u53ef\u4ee5\u5b9e\u73b0\u589e\u91cf\u6536\u96c6\uff0c\u53ea\u8981\u5f15\u7528\u8ba1\u6570\u4e3a\u96f6\u5c31\u56de\u6536\uff0c\u907f\u514d\u51fa\u73b0\u5468\u671f\u6027\u7684\u6682\u505c\uff1b\u7f3a\u70b9\u662f\u9700\u8981\u89e3\u51b3\u5faa\u73af\u5f15\u7528\u95ee\u9898\uff0c\u5e76\u4e14\u8981\u7ecf\u5e38\u4fee\u6539\u5f15\u7528\u8ba1\u6570\uff08\u6bd4\u5982\u5728\u6bcf\u6b21\u8d4b\u503c\u548c\u53d8\u91cf\u8d85\u51fa\u4f5c\u7528\u57df\u7684\u65f6\u5019\uff09\uff0c\u5f00\u9500\u6709\u70b9\u5927\u3002<\/p>\n<p><strong>3. \u552f\u4e00 ID<\/strong><\/p>\n<p>\u6bcf\u4e2a Python \u5bf9\u8c61\u90fd\u6709\u4e00\u4e2a\u552f\u4e00 ID\uff0c\u5b83\u4eec\u5728\u751f\u5b58\u671f\u5185\u662f\u4e0d\u53d8\u7684\u3002\u7528 id() \u51fd\u6570\u5c31\u53ef\u4ee5\u83b7\u5f97\u5bf9\u8c61\u7684 ID\u3002\u6839\u636e Python \u7684\u6587\u6863\uff0c\u8fd9\u4e2a ID \u5b9e\u9645\u5c31\u662f\u5bf9\u8c61\u7684\u5185\u5b58\u5730\u5740\u3002\u6240\u4ee5\uff0c\u5b9e\u9645\u4e0a\uff0c\u4f60\u4e0d\u9700\u8981\u5728\u5bf9\u8c61\u91cc\u7528\u4e00\u4e2a\u5355\u72ec\u7684\u5b57\u6bb5\u6765\u8bb0\u5f55\u5bf9\u8c61 ID\u3002\u8fd9\u540c\u65f6\u4e5f\u8bf4\u660e\uff0cPython \u5bf9\u8c61\u7684\u5730\u5740\u5728\u6574\u4e2a\u751f\u547d\u5468\u671f\u5185\u662f\u4e0d\u4f1a\u53d8\u7684\uff0c\u8fd9\u4e5f\u7b26\u5408\u57fa\u4e8e\u5f15\u7528\u8ba1\u6570\u7684\u5783\u573e\u6536\u96c6\u7b97\u6cd5\u3002\u5bf9\u6bd4\u4e00\u4e0b\uff0c\u5982\u679c\u91c7\u7528\u201c\u505c\u6b62\u548c\u62f7\u8d1d\u201d\u7684\u7b97\u6cd5\uff0c\u5bf9\u8c61\u5728\u5185\u5b58\u4e2d\u4f1a\u88ab\u79fb\u52a8\uff0c\u5730\u5740\u4f1a\u53d1\u751f\u53d8\u5316\u3002\u6240\u4ee5\u4f60\u80fd\u770b\u51fa\uff0cID \u7684\u7b97\u6cd5\u4e0e\u5783\u573e\u6536\u96c6\u7b97\u6cd5\u662f\u73af\u73af\u76f8\u6263\u7684\u3002<\/p>\n<pre><code>>&gt;&gt; a = 10\n>&gt;&gt; id(a)\n140330839057200<\/code><\/pre>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u770b\u770b ob_type \u8fd9\u4e2a\u5b57\u6bb5\uff0c\u5b83\u6307\u5411\u7684\u662f\u5bf9\u8c61\u7684\u7c7b\u578b\u3002\u4ee5\u8fd9\u4e2a\u5b57\u6bb5\u4e3a\u7ebf\u7d22\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u7275\u51fa Python \u7684\u6574\u4e2a\u7c7b\u578b\u7cfb\u7edf\u7684\u8bbe\u8ba1\u3002<\/p>\n<h2>Python \u7684\u7c7b\u578b\u7cfb\u7edf<\/h2>\n<p>Python \u662f\u52a8\u6001\u7c7b\u578b\u7684\u8bed\u8a00\u3002\u5b83\u7684\u7c7b\u578b\u7cfb\u7edf\u7684\u8bbe\u8ba1\u76f8\u5f53\u7cbe\u5de7\uff0cPython \u8bed\u8a00\u7684\u5f88\u591a\u4f18\u70b9\uff0c\u90fd\u6765\u81ea\u4e8e\u5b83\u7684\u7c7b\u578b\u7cfb\u7edf\u3002\u6211\u4eec\u6765\u770b\u4e00\u4e0b\u3002<\/p>\n<p>\u9996\u5148\uff0cPython \u91cc\u6bcf\u4e2a PyObject \u5bf9\u8c61\u90fd\u6709\u4e00\u4e2a\u7c7b\u578b\u4fe1\u606f\u3002\u4fdd\u5b58\u7c7b\u578b\u4fe1\u606f\u7684\u6570\u636e\u7ed3\u6784\u662f PyTypeObject\uff08\u5b9a\u4e49\u5728 Include\/cpython\/object.h \u4e2d\uff09\u3002PyTypeObject \u672c\u8eab\u4e5f\u662f\u4e00\u4e2a PyObject\uff0c\u53ea\u4e0d\u8fc7\u8fd9\u4e2a\u5bf9\u8c61\u662f\u7528\u4e8e\u8bb0\u5f55\u7c7b\u578b\u4fe1\u606f\u7684\u800c\u5df2\u3002\u5b83\u662f\u4e00\u4e2a\u633a\u5927\u7684\u7ed3\u6784\u4f53\uff0c\u5305\u542b\u4e86\u5bf9\u4e00\u4e2a\u7c7b\u578b\u7684\u5404\u79cd\u63cf\u8ff0\u4fe1\u606f\uff0c\u4e5f\u5305\u542b\u4e86\u4e00\u4e9b\u51fd\u6570\u7684\u6307\u9488\uff0c\u8fd9\u4e9b\u51fd\u6570\u5c31\u662f\u5bf9\u8be5\u7c7b\u578b\u53ef\u4ee5\u505a\u7684\u64cd\u4f5c\u3002\u53ef\u4ee5\u8bf4\uff0c\u53ea\u8981\u4f60\u5bf9\u8fd9\u4e2a\u7ed3\u6784\u4f53\u7684\u6bcf\u4e2a\u5b57\u6bb5\u7684\u4f5c\u7528\u90fd\u4e86\u89e3\u6e05\u695a\u4e86\uff0c\u90a3\u4e48\u4f60\u5bf9 Python \u7684\u7c7b\u578b\u4f53\u7cfb\u4e5f\u5c31\u4e86\u89e3\u900f\u5f7b\u4e86\u3002<\/p>\n<pre><code class=\"language-c\">typedef struct _typeobject {\n    PyObject_VAR_HEAD\n    const char *tp_name;  \/* \u7528\u4e8e\u6253\u5370\u7684\u540d\u79f0\u683c\u5f0f\u662f&quot;&lt;\u6a21\u5757&gt;.&lt;\u540d\u79f0&gt;&quot; *\/\n    Py_ssize_t tp_basicsize, tp_itemsize; \/* \u7528\u4e8e\u7533\u8bf7\u5185\u5b58 *\/\n\n    \/* \u540e\u9762\u8fd8\u6709\u5f88\u591a\u5b57\u6bb5\uff0c\u6bd4\u5982\u7528\u4e8e\u652f\u6301\u6570\u503c\u8ba1\u7b97\u3001\u5e8f\u5217\u3001\u6620\u5c04\u7b49\u64cd\u4f5c\u7684\u51fd\u6570\uff0c\u7528\u4e8e\u63cf\u8ff0\u5c5e\u6027\u3001\u5b50\u7c7b\u578b\u3001\u6587\u6863\u7b49\u5185\u5bb9\u7684\u5b57\u6bb5\uff0c\u7b49\u7b49\u3002 *\/\n    ...  \n} PyTypeObject<\/code><\/pre>\n<p>\u56e0\u4e3a\u8fd9\u4e2a\u7ed3\u6784\u975e\u5e38\u91cd\u8981\uff0c\u6240\u4ee5\u6211\u628a\u4e00\u4e9b\u6709\u4ee3\u8868\u6027\u7684\u5b57\u6bb5\u6574\u7406\u4e86\u4e00\u4e0b\uff0c\u4f60\u53ef\u4ee5\u91cd\u70b9\u5173\u6ce8\u5b83\u4eec\uff1a<\/p>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/%E4%B8%8B%E8%BD%BD.jpg\" alt=\"\u4e0b\u8f7d\" \/><\/p>\n<p>\u4f60\u4f1a\u770b\u5230\uff0c\u8fd9\u4e2a\u7ed3\u6784\u91cc\u7684\u5f88\u591a\u90e8\u5206\u90fd\u662f\u4e00\u4e2a\u51fd\u6570\u63d2\u69fd\uff08Slot\uff09\uff0c\u4f60\u53ef\u4ee5\u5f80\u63d2\u69fd\u91cc\u4fdd\u5b58\u4e00\u4e9b\u51fd\u6570\u6307\u9488\uff0c\u7528\u6765\u5b9e\u73b0\u5404\u79cd\u6807\u51c6\u64cd\u4f5c\uff0c\u6bd4\u5982\u5bf9\u8c61\u751f\u547d\u5468\u671f\u7ba1\u7406\u3001\u8f6c\u6210\u5b57\u7b26\u4e32\u3001\u83b7\u53d6\u54c8\u5e0c\u503c\u7b49\u3002<\/p>\n<p>\u5728\u4e0a\u9762\u7684\u8868\u683c\u4e2d\uff0c\u4f60\u8fd8\u4f1a\u770b\u5230\u50cf\u201c<strong>init<\/strong>\u201d\u8fd9\u6837\u7684\u65b9\u6cd5\uff0c\u5b83\u7684\u4e24\u8fb9\u90fd\u662f\u6709\u4e24\u4e2a\u4e0b\u5212\u7ebf\u7684\uff0c\u4e5f\u5c31\u662f\u201cdouble underscore\u201d\uff0c\u7b80\u79f0 dunder \u65b9\u6cd5\uff0c\u4e5f\u53eb\u505a\u201c<strong>\u9b54\u672f\u65b9\u6cd5<\/strong>\u201d\u3002\u5728\u7528 Python \u7f16\u5199\u81ea\u5b9a\u4e49\u7684\u7c7b\u7684\u65f6\u5019\uff0c\u4f60\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u4e9b\u9b54\u672f\u65b9\u6cd5\uff0c\u5b83\u4eec\u5c31\u4f1a\u88ab\u7f3a\u7701\u7684tp_*\u51fd\u6570\u6240\u8c03\u7528\uff0c\u6bd4\u5982\uff0c\u201c<strong>init<\/strong>\u201d\u4f1a\u88ab\u7f3a\u7701\u7684tp_init\u51fd\u6570\u8c03\u7528\uff0c\u5b8c\u6210\u7c7b\u7684\u521d\u59cb\u5316\u5de5\u4f5c\u3002<\/p>\n<p>\u73b0\u5728\u6211\u4eec\u62ff\u6574\u578b\u5bf9\u8c61\u6765\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u4e00\u8d77\u6765\u770b\u770b\u5b83\u7684 PyTypeObject \u662f\u4ec0\u4e48\u6837\u5b50\u3002<\/p>\n<p>\u5bf9\u4e8e\u6574\u578b\u5bf9\u8c61\u6765\u8bf4\uff0c\u5b83\u7684 ob_type \u4f1a\u6307\u5411\u4e00\u4e2a PyLong_Type \u5bf9\u8c61\u3002\u8fd9\u4e2a\u5bf9\u8c61\u662f\u5728 longobject.c \u4e2d\u521d\u59cb\u5316\u7684\uff0c\u5b83\u662f PyTypeObject \u7684\u4e00\u4e2a\u5b9e\u4f8b\u3002\u4ece\u4e2d\u4f60\u4f1a\u770b\u5230\u4e00\u4e9b\u4fe1\u606f\uff1a\u7c7b\u578b\u540d\u79f0\u662f\u201cint\u201d\uff0c\u8f6c\u5b57\u7b26\u4e32\u7684\u51fd\u6570\u662flong_to_decimal_string\uff0c\u6b64\u5916\u8fd8\u6709\u6bd4\u8f83\u51fd\u6570\u3001\u65b9\u6cd5\u63cf\u8ff0\u3001\u5c5e\u6027\u63cf\u8ff0\u3001\u6784\u5efa\u548c\u6790\u6784\u51fd\u6570\u7b49\u3002<\/p>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/%E4%B8%8B%E8%BD%BD%20(1).jpg\" alt=\"\u4e0b\u8f7d (1)\" \/><\/p>\n<p>\u6211\u4eec\u8fd0\u884c type() \u51fd\u6570\uff0c\u53ef\u4ee5\u83b7\u5f97\u4e00\u4e2a\u5bf9\u8c61\u7684\u7c7b\u578b\u540d\u79f0\uff0c\u8fd9\u4e2a\u540d\u79f0\u5c31\u6765\u81ea PyTypeObject \u7684tp_name\u3002<\/p>\n<pre><code>>&gt;&gt; a = 10\n>&gt;&gt; type(a)\n&lt;type &#039;int&#039;&gt;<\/code><\/pre>\n<p>\u6211\u4eec\u7528 dir() \u51fd\u6570\uff0c\u53ef\u4ee5\u4ece PyTypeObject \u4e2d\u67e5\u8be2\u51fa\u4e00\u4e2a\u5bf9\u8c61\u6240\u652f\u6301\u7684\u6240\u6709\u5c5e\u6027\u548c\u65b9\u6cd5\u3002\u6bd4\u5982\uff0c\u4e0b\u9762\u662f\u67e5\u8be2\u4e00\u4e2a\u6574\u578b\u5bf9\u8c61\u83b7\u5f97\u7684\u7ed3\u679c\uff1a<\/p>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20230829143610308.png\" alt=\"image-20230829143610308\" \/><\/p>\n<p>\u597d\uff0c\u6211\u4eec\u521a\u624d\u8bb2\u4e86\u6574\u578b\uff0c\u5b83\u5bf9\u5e94\u7684 PyTypeObject \u7684\u5b9e\u4f8b\u662f PyLong_Type\u3002Python \u91cc\u5176\u5b9e\u8fd8\u6709\u5176\u4ed6\u4e00\u4e9b\u5185\u7f6e\u7684\u7c7b\u578b\uff0c\u5b83\u4eec\u5206\u522b\u90fd\u5bf9\u5e94\u4e86\u4e00\u4e2a PyTypeObject \u7684\u5b9e\u4f8b\u3002\u4f60\u53ef\u4ee5\u53c2\u8003\u4e00\u4e0b\u8fd9\u4e2a\u8868\u683c\u3002<\/p>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20230829143636401.png\" alt=\"image-20230829143636401\" \/><\/p>\n<p>\u4e0a\u9762\u5217\u51fa\u7684\u8fd9\u4e9b\u90fd\u662f Python \u7684\u5185\u7f6e\u7c7b\u578b\u3002\u6709\u4e9b\u5185\u7f6e\u7c7b\u578b\u8ddf\u8bed\u6cd5\u662f\u5173\u8054\u7740\u7684\uff0c\u6bd4\u5982\u8bf4\uff0c\u201ca = 1\u201d\u5c31\u4f1a\u81ea\u52a8\u521b\u5efa\u4e00\u4e2a\u6574\u578b\u5bf9\u8c61\uff0c\u201ca = [2, 'john', 3]\u201d\u5c31\u4f1a\u81ea\u52a8\u521b\u5efa\u4e00\u4e2a List \u5bf9\u8c61\u3002<strong>\u8fd9\u4e9b\u5185\u7f6e\u5bf9\u8c61\u90fd\u662f\u7528 C \u8bed\u8a00\u5b9e\u73b0\u7684\u3002<\/strong><\/p>\n<p><strong>Python \u6bd4\u8f83\u6709\u4f18\u52bf\u7684\u4e00\u70b9\uff0c\u662f\u4f60\u53ef\u4ee5\u7528 C \u8bed\u8a00\uff0c\u50cf\u5b9e\u73b0\u5185\u7f6e\u7c7b\u578b\u4e00\u6837\u5b9e\u73b0\u81ea\u5df1\u7684\u7c7b\u578b\uff0c\u5e76\u62e5\u6709\u5f88\u9ad8\u7684\u6027\u80fd\u3002<\/strong><\/p>\n<p>\u5f53\u7136\uff0c\u5982\u679c\u6027\u80fd\u4e0d\u662f\u4f60\u6700\u91cd\u8981\u7684\u8003\u8651\u56e0\u7d20\uff0c\u90a3\u4e48\u4f60\u4e5f\u53ef\u4ee5\u7528 Python \u6765\u521b\u5efa\u65b0\u7684\u7c7b\u578b\uff0c\u4e5f\u5c31\u662f\u4ee5 class \u5173\u952e\u5b57\u5f00\u5934\u7684\u81ea\u5b9a\u4e49\u7c7b\u3002class \u7f16\u8bd1\u4ee5\u540e\uff0c\u4e5f\u4f1a\u5f62\u6210\u4e00\u4e2a PyTypeObject \u5bf9\u8c61\uff0c\u6765\u4ee3\u8868\u8fd9\u4e2a\u7c7b\u3002\u4f60\u4e3a\u8fd9\u4e2a\u7c7b\u7f16\u5199\u7684\u5404\u79cd\u5c5e\u6027\uff08\u6bd4\u5982\u7c7b\u540d\u79f0\uff09\u548c\u65b9\u6cd5\uff0c\u4f1a\u88ab\u5b58\u5230\u7c7b\u578b\u5bf9\u8c61\u4e2d\u3002<\/p>\n<p>\u597d\u4e86\uff0c\u73b0\u5728\u4f60\u5df2\u7ecf\u521d\u6b65\u4e86\u89e3\u4e86 Python \u7684\u7c7b\u578b\u7cfb\u7edf\u7684\u7279\u5f81\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u5c31\u5e26\u4f60\u66f4\u6df1\u5165\u5730\u4e86\u89e3\u4e00\u4e0b\u7c7b\u578b\u5bf9\u8c61\u4e2d\u4e00\u4e9b\u91cd\u8981\u7684\u51fd\u6570\u63d2\u69fd\u7684\u4f5c\u7528\uff0c\u4ee5\u53ca\u5b83\u4eec\u6240\u6784\u6210\u7684\u4e00\u4e9b\u534f\u8bae\u3002<\/p>\n<h2>Python \u5bf9\u8c61\u7684\u4e00\u4e9b\u534f\u8bae<\/h2>\n<p>\u524d\u9762\u5728\u7814\u7a76\u6574\u578b\u5bf9\u8c61\u7684\u65f6\u5019\uff0c\u4f60\u4f1a\u53d1\u73b0 PyLong_Type \u7684 tp_as_number \u5b57\u6bb5\u88ab\u8d4b\u503c\u4e86\uff0c\u8fd9\u662f\u4e00\u4e2a\u7ed3\u6784\u4f53\uff08PyNumberMethods\uff09\uff0c\u91cc\u9762\u662f\u5f88\u591a\u4e0e\u6570\u503c\u8ba1\u7b97\u6709\u5173\u7684\u51fd\u6570\u6307\u9488\uff0c\u5305\u62ec\u52a0\u51cf\u4e58\u9664\u7b49\u3002\u8fd9\u4e9b\u51fd\u6570\u6307\u9488\u662f\u5b9e\u73b0 Python \u7684\u6570\u503c\u8ba1\u7b97\u65b9\u9762\u7684\u534f\u8bae\u3002\u4efb\u4f55\u7c7b\u578b\uff0c\u53ea\u8981\u63d0\u4f9b\u4e86\u8fd9\u4e9b\u51fd\u6570\uff0c\u5c31\u53ef\u4ee5\u50cf\u6574\u578b\u5bf9\u8c61\u4e00\u6837\u8fdb\u884c\u8ba1\u7b97\u3002\u8fd9\u5b9e\u9645\u4e0a\u662f Python \u5b9a\u4e49\u7684\u4e00\u4e2a\u9488\u5bf9\u6570\u503c\u8ba1\u7b97\u7684\u534f\u8bae\u3002<\/p>\n<p>\u65e2\u7136\u5982\u6b64\uff0c\u6211\u4eec\u518d\u6b21\u7528 GDB \u6765\u8ddf\u8e2a\u4e00\u4e0b Python \u7684\u6267\u884c\u8fc7\u7a0b\uff0c\u770b\u770b\u6574\u6570\u7684\u52a0\u6cd5\u662f\u600e\u4e48\u5b9e\u73b0\u7684\u3002\u6211\u4eec\u7684\u793a\u4f8b\u7a0b\u5e8f\u589e\u52a0\u4e86\u4e00\u884c\u4ee3\u7801\uff0c\u53d8\u6210\uff1a<\/p>\n<pre><code>a = 1\nb = a + 2<\/code><\/pre>\n<p>\u5b83\u5bf9\u5e94\u7684\u5b57\u8282\u7801\u5982\u4e0b\uff1a<\/p>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20230829143713803.png\" alt=\"image-20230829143713803\" \/><\/p>\n<p>\u6211\u4eec\u91cd\u70b9\u6765\u5173\u6ce8 BINARY_ADD \u6307\u4ee4\u7684\u6267\u884c\u60c5\u51b5\uff0c\u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/p>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20230829143737363.png\" alt=\"image-20230829143737363\" \/><\/p>\n<p>\u53ef\u4ee5\u770b\u5230\uff0c\u5982\u679c + \u53f7\u4e24\u8fb9\u662f\u5b57\u7b26\u4e32\uff0c\u90a3\u4e48\u7f16\u8bd1\u5668\u5c31\u4f1a\u6267\u884c\u5b57\u7b26\u4e32\u8fde\u63a5\u64cd\u4f5c\u3002\u5426\u5219\uff0c\u5c31\u4f5c\u4e3a\u6570\u5b57\u76f8\u52a0\u3002<\/p>\n<p>\u6211\u4eec\u7ee7\u7eed\u8ddf\u8e2a\u8fdb\u5165 PyNumber_Add \u51fd\u6570\u3002\u5728\u8fd9\u4e2a\u51fd\u6570\u4e2d\uff0cPython \u6c42\u51fa\u4e86\u52a0\u6cd5\u51fd\u6570\u6307\u9488\u5728 PyNumberMethods \u7ed3\u6784\u4f53\u4e2d\u7684\u504f\u79fb\u91cf\uff0c\u63a5\u7740\u5c31\u8fdb\u5165\u4e86 binary_op1() \u51fd\u6570\u3002<\/p>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20230829143802689.png\" alt=\"image-20230829143802689\" \/><\/p>\n<p>\u5728 binary_op1 \u51fd\u6570\u4e2d\uff0cPython \u9996\u5148\u4ece\u7b2c\u4e00\u4e2a\u53c2\u6570\u7684\u7c7b\u578b\u5bf9\u8c61\u4e2d\uff0c\u53d6\u51fa\u4e86\u52a0\u6cd5\u51fd\u6570\u7684\u6307\u9488\u3002\u4f60\u5728 GDB \u4e2d\u6253\u5370\u51fa\u8f93\u51fa\u4fe1\u606f\uff0c\u5c31\u4f1a\u53d1\u73b0\u5b83\u662f binaryfunc \u7c7b\u578b\u7684\uff0c\u51fd\u6570\u540d\u79f0\u662f long_add\u3002<\/p>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20230829143825239.png\" alt=\"image-20230829143825239\" \/><\/p>\n<p>binaryfunc \u7c7b\u578b\u7684\u5b9a\u4e49\u662f\uff1a<\/p>\n<pre><code class=\"language-c\">typedef PyObject * (*binaryfunc)(PyObject *, PyObject *);<\/code><\/pre>\n<p>\u4e5f\u5c31\u662f\u8bf4\uff0c\u5b83\u662f\u6307\u5411\u7684\u51fd\u6570\u8981\u6c42\u6709\u4e24\u4e2a Python \u5bf9\u8c61\uff08\u7684\u6307\u9488\uff09\u4f5c\u4e3a\u53c2\u6570\uff0c\u8fd4\u56de\u503c\u4e5f\u662f\u4e00\u4e2a Python \u5bf9\u8c61\uff08\u7684\u6307\u9488\uff09\u3002<\/p>\n<p>\u4f60\u518d\u7ee7\u7eed\u8ddf\u8e2a\u4e0b\u53bb\uff0c\u4f1a\u53d1\u73b0\u7a0b\u5e8f\u5c31\u8fdb\u5165\u5230\u4e86 long_add \u51fd\u6570\u3002\u8fd9\u4e2a\u51fd\u6570\u662f\u5728 longobject.c \u91cc\u5b9a\u4e49\u7684\uff0c\u662f Python \u6574\u578b\u7c7b\u578b\u505a\u52a0\u6cd5\u8ba1\u7b97\u7684\u5185\u7f6e\u51fd\u6570\u3002<\/p>\n<p>\u8fd9\u91cc\u6709\u4e00\u4e2a\u9690\u79d8\u7684\u95ee\u9898\uff0c<strong>\u4e3a\u4ec0\u4e48\u662f\u4f7f\u7528\u4e86\u7b2c\u4e00\u4e2a\u53c2\u6570\uff08\u4e5f\u5c31\u662f\u52a0\u6cd5\u5de6\u8fb9\u7684\u5bf9\u8c61\uff09\u6240\u5173\u8054\u7684\u52a0\u6cd5\u51fd\u6570\uff0c\u800c\u4e0d\u662f\u7b2c\u4e8c\u4e2a\u53c2\u6570\u7684\u52a0\u6cd5\u51fd\u6570\uff1f<\/strong><\/p>\n<p>\u5728\u6211\u4eec\u7684\u793a\u4f8b\u7a0b\u5e8f\u4e2d\uff0c\u7531\u4e8e\u52a0\u6cd5\u4e24\u8fb9\u7684\u5bf9\u8c61\u7684\u7c7b\u578b\u662f\u76f8\u540c\u7684\uff0c\u90fd\u662f\u6574\u578b\uff0c\u6240\u4ee5\u5b83\u4eec\u6240\u5173\u8054\u7684\u52a0\u6cd5\u51fd\u6570\u662f\u540c\u4e00\u4e2a\u3002\u4f46\u662f\uff0c\u5982\u679c\u4e24\u8fb9\u7684\u7c7b\u578b\u4e0d\u4e00\u6837\u600e\u4e48\u529e\u5462\uff1f\u8fd9\u4e2a\u5176\u5b9e\u662f\u4e00\u4e2a\u5f88\u6709\u610f\u601d\u7684\u51fd\u6570\u5206\u6d3e\u95ee\u9898\uff0c\u4f60\u53ef\u4ee5\u5148\u601d\u8003\u4e00\u4e0b\u7b54\u6848\uff0c\u6211\u4f1a\u5728\u540e\u9762\u8bb2 Julia \u7684\u7f16\u8bd1\u5668\u65f6\u518d\u56de\u5230\u8fd9\u4e2a\u95ee\u9898\u4e0a\u3002<\/p>\n<p>\u597d\u4e86\uff0c\u73b0\u5728\u6211\u4eec\u5c31\u80fd\u7406\u89e3\u4e86\uff0c\u50cf\u52a0\u51cf\u4e58\u9664\u8fd9\u6837\u8fd0\u7b97\uff0c\u5b83\u4eec\u5728 Python \u91cc\u90fd\u662f\u600e\u4e48\u5b9e\u73b0\u7684\u4e86\u3002Python \u662f\u5230\u5bf9\u8c61\u7684\u7c7b\u578b\u4e2d\uff0c\u53bb\u67e5\u627e\u9488\u5bf9\u8fd9\u4e9b\u8fd0\u7b97\u7684\u51fd\u6570\u6765\u6267\u884c\u3002<\/p>\n<p>\u9664\u4e86\u5185\u7f6e\u7684\u51fd\u6570\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u81ea\u5df1\u5199\u8fd9\u6837\u7684\u51fd\u6570\uff0c\u5e76\u88ab Python \u6240\u8c03\u7528\u3002\u6765\u770b\u770b\u4e0b\u9762\u7684\u793a\u4f8b\u7a0b\u5e8f\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u201c<strong>add<\/strong>\u201d\u9b54\u672f\u65b9\u6cd5\u3002\u8fd9\u4e2a\u65b9\u6cd5\u4f1a\u88ab Python \u4f5c\u4e3a SimpleComplex \u7684\u52a0\u6cd5\u51fd\u6570\u6240\u4f7f\u7528\uff0c\u5b9e\u73b0\u4e86\u52a0\u6cd5\u64cd\u4f5c\u7b26\u7684\u91cd\u8f7d\uff0c\u4ece\u800c\u652f\u6301\u590d\u6570\u7684\u52a0\u6cd5\u64cd\u4f5c\u3002<\/p>\n<pre><code class=\"language-python\">class SimpleComplex(object):\n    def __init__(self, x, y):\n        self.x = x\n        self.y = y\n    def __str__(self):  \n        return &quot;x: %d, y: %d&quot; % (self.x, self.y)\n    def __add__(self, other):\n        return SimpleComplex(self.x + other.x, self.y + other.y)\na = SimpleComplex(1, 2)\nb = SimpleComplex(3, 4)\nc = a + b\nprint(c)<\/code><\/pre>\n<p><strong>\u90a3\u4e48\u5bf9\u4e8e\u8fd9\u4e48\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7c7b\uff0c\u5728\u6267\u884c BINARY_ADD \u6307\u4ee4\u65f6\u4f1a\u6709\u4ec0\u4e48\u4e0d\u540c\u5462\uff1f<\/strong>\u901a\u8fc7 GDB \u505a\u8ddf\u8e2a\uff0c\u4f60\u4f1a\u53d1\u73b0\u51e0\u70b9\u4e0d\u540c\uff1a<\/p>\n<p>\u9996\u5148\uff0c\u5728 SimpleComplex \u7684 type \u5bf9\u8c61\u4e2d\uff0cadd \u51fd\u6570\u63d2\u69fd\u91cc\u653e\u4e86\u4e00\u4e2a slot_nb_add() \u51fd\u6570\u6307\u9488\uff0c\u8fd9\u4e2a\u51fd\u6570\u4f1a\u5230\u5bf9\u8c61\u91cc\u67e5\u627e\u201c<strong>add<\/strong>\u201d\u51fd\u6570\u3002\u56e0\u4e3a Python \u7684\u4e00\u5207\u90fd\u662f\u5bf9\u8c61\uff0c\u56e0\u6b64\u5b83\u627e\u5230\u7684\u662f\u4e00\u4e2a\u51fd\u6570\u5bf9\u8c61\u3002<\/p>\n<p>\u6240\u4ee5\uff0c\u63a5\u4e0b\u6765\uff0cPython \u9700\u8981\u8fd0\u884c\u8fd9\u4e2a\u51fd\u6570\u5bf9\u8c61\uff0c\u800c\u4e0d\u662f\u7528 C \u8bed\u8a00\u5199\u7684\u5185\u7f6e\u51fd\u6570\u3002<strong>\u90a3\u4e48\u600e\u4e48\u8fd0\u884c\u8fd9\u4e2a\u51fd\u6570\u5bf9\u8c61\u5462\uff1f<\/strong><\/p>\n<p>\u8fd9\u5c31\u9700\u8981\u7528\u5230 Python \u7684\u53e6\u4e00\u4e2a\u534f\u8bae\uff0c<strong>Callable \u534f\u8bae<\/strong>\u3002\u8fd9\u4e2a\u534f\u8bae\u89c4\u5b9a\uff0c\u53ea\u8981\u4e3a\u5bf9\u8c61\u7684\u7c7b\u578b\u4e2d\u7684 tp_call \u5c5e\u6027\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5408\u6cd5\u7684\u51fd\u6570\uff0c\u90a3\u4e48\u8be5\u5bf9\u8c61\u5c31\u662f\u53ef\u88ab\u8c03\u7528\u7684\u3002<\/p>\n<p>\u5bf9\u4e8e\u81ea\u5b9a\u4e49\u7684\u51fd\u6570\uff0cPython \u4f1a\u8bbe\u7f6e\u4e00\u4e2a\u7f3a\u7701\u7684 tp_call \u51fd\u6570\u3002\u8fd9\u4e2a\u51fd\u6570\u6240\u505a\u7684\u4e8b\u60c5\uff0c\u5b9e\u9645\u4e0a\u5c31\u662f\u627e\u5230\u8be5\u51fd\u6570\u6240\u7f16\u8bd1\u51fa\u6765\u7684 PyCodeObject\uff0c\u5e76\u8ba9\u89e3\u91ca\u5668\u6267\u884c\u5176\u4e2d\u7684\u5b57\u8282\u7801\uff01<\/p>\n<p>\u597d\u4e86\uff0c\u901a\u8fc7\u4e0a\u9762\u7684\u793a\u4f8b\u7a0b\u5e8f\uff0c\u6211\u4eec\u52a0\u6df1\u4e86\u5bf9\u7c7b\u578b\u5bf9\u8c61\u7684\u4e86\u89e3\uff0c\u4e5f\u4e86\u89e3\u4e86 Python \u5173\u4e8e\u6570\u503c\u8ba1\u7b97\u548c\u53ef\u8c03\u7528\u6027\uff08Callable\uff09\u65b9\u9762\u7684\u534f\u8bae\u3002<\/p>\n<p>Python \u8fd8\u6709\u5176\u4ed6\u51e0\u4e2a\u534f\u8bae\uff0c\u6bd4\u5982\u679a\u4e3e\u534f\u8bae\u548c\u6620\u5c04\u534f\u8bae\u7b49\uff0c\u7528\u6765\u652f\u6301\u5bf9\u8c61\u7684\u679a\u4e3e\u3001\u628a\u5bf9\u8c61\u52a0\u5165\u5b57\u5178\u7b49\u64cd\u4f5c\u3002\u4f60\u53ef\u4ee5\u5229\u7528\u8fd9\u4e9b\u534f\u8bae\uff0c\u5145\u5206\u878d\u5165\u5230 Python \u8bed\u8a00\u7684\u6846\u67b6\u4e2d\uff0c\u6bd4\u5982\u8bf4\uff0c\u4f60\u53ef\u4ee5\u91cd\u8f7d\u52a0\u51cf\u4e58\u9664\u7b49\u8fd0\u7b97\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u518d\u8fd0\u7528 Callable \u534f\u8bae\u7684\u77e5\u8bc6\uff0c\u6765\u63a2\u7a76\u4e00\u4e0b Python \u5bf9\u8c61\u7684\u521b\u5efa\u673a\u5236\u3002<\/p>\n<h2>Python \u5bf9\u8c61\u7684\u521b\u5efa<\/h2>\n<p>\u7528 Python \u8bed\u8a00\uff0c\u6211\u4eec\u53ef\u4ee5\u7f16\u5199 class\uff0c\u6765\u652f\u6301\u81ea\u5b9a\u4e49\u7684\u7c7b\u578b\u3002\u6211\u4eec\u6765\u770b\u4e00\u6bb5\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">class myclass:\n    def __init__(self, x):\n        self.x = x    \n    def foo(self, b):\n        c = self.x + b\n        return c\na = myclass(2);\n<\/code><\/pre>\n<p>\u5176\u4e2d\uff0cmyclass(2) \u662f\u751f\u6210\u4e86\u4e00\u4e2a myclass \u5bf9\u8c61\u3002<\/p>\n<p>\u53ef\u662f\uff0c\u4f60\u53d1\u73b0\u6ca1\u6709\uff0cPython \u521b\u5efa\u4e00\u4e2a\u5bf9\u8c61\u5b9e\u4f8b\u7684\u65b9\u5f0f\uff0c\u5176\u5b9e\u8ddf\u8c03\u7528\u4e00\u4e2a\u51fd\u6570\u6ca1\u5565\u533a\u522b\uff08\u4e0d\u50cf Java \u8bed\u8a00\uff0c\u8fd8\u9700\u8981 new \u5173\u952e\u5b57\uff09\u3002\u5982\u679c\u4f60\u4e0d\u77e5\u9053 myclass \u662f\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684\u7c7b\uff0c\u4f60\u4f1a\u4ee5\u4e3a\u53ea\u662f\u5728\u8c03\u7528\u4e00\u4e2a\u51fd\u6570\u800c\u5df2\u3002<\/p>\n<p>\u4e0d\u8fc7\uff0c\u6211\u4eec\u524d\u9762\u5df2\u7ecf\u63d0\u5230\u4e86 Python \u7684 Callable \u534f\u8bae\u3002\u6240\u4ee5\uff0c\u5229\u7528\u8fd9\u4e2a\u534f\u8bae\uff0c\u4efb\u4f55\u5bf9\u8c61\u53ea\u8981\u5728\u5176\u7c7b\u578b\u4e2d\u5b9a\u4e49\u4e86 tp_call\uff0c\u90a3\u4e48\u5c31\u90fd\u53ef\u4ee5\u88ab\u8c03\u7528\u3002<\/p>\n<p>\u6211\u518d\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u52a0\u6df1\u4f60\u5bf9 Callable \u534f\u8bae\u7684\u7406\u89e3\u3002\u5728\u4e0b\u9762\u7684\u793a\u4f8b\u7a0b\u5e8f\u4e2d\uff0c\u6211\u5b9a\u4e49\u4e86\u4e00\u4e2a\u7c7b\u578b Bar\uff0c\u5e76\u521b\u5efa\u4e86\u4e00\u4e2a\u5bf9\u8c61 b\u3002<\/p>\n<pre><code class=\"language-python\">class Bar:\n    def __call__(self):\n            print(&quot;in __call__: &quot;, self)\nb = Bar()\nb()                              #\u8fd9\u91cc\u4f1a\u6253\u5370\u5bf9\u8c61\u4fe1\u606f\uff0c\u5e76\u663e\u793a\u5bf9\u8c61\u5730\u5740 <\/code><\/pre>\n<p>\u73b0\u5728\uff0c\u6211\u5728 b \u5bf9\u8c61\u540e\u9762\u52a0\u4e00\u5bf9\u62ec\u53f7\uff0c\u5c31\u53ef\u4ee5\u8c03\u7528 b \u4e86\uff01\u5b9e\u9645\u6267\u884c\u7684\u5c31\u662f Bar \u7684\u201c<strong>call<\/strong>\u201d\u51fd\u6570\uff08\u7f3a\u7701\u7684 tp_call \u51fd\u6570\u4f1a\u67e5\u627e\u201c<strong>call<\/strong>\u201d\u5c5e\u6027\uff0c\u5e76\u8c03\u7528\uff09\u3002<\/p>\n<p>\u6240\u4ee5\uff0c\u6211\u4eec\u8c03\u7528 myclass()\uff0c\u90a3\u4e00\u5b9a\u662f\u56e0\u4e3a myclass \u7684\u7c7b\u578b\u5bf9\u8c61\u4e2d\u5b9a\u4e49\u4e86 tp_call\u3002<\/p>\n<p>\u4f60\u8fd8\u53ef\u4ee5\u628a\u201cmyclass(2)\u201d\u8fd9\u4e2a\u8bed\u53e5\u7f16\u8bd1\u6210\u5b57\u8282\u7801\u770b\u770b\uff0c\u5b83\u751f\u6210\u7684\u662f CALL_FUNCTION \u6307\u4ee4\uff0c\u4e0e\u51fd\u6570\u8c03\u7528\u6ca1\u6709\u4efb\u4f55\u533a\u522b\u3002<\/p>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20230829143948595.png\" alt=\"image-20230829143948595\" \/><\/p>\n<p>\u53ef\u662f\uff0c\u6211\u4eec\u77e5\u9053\uff0c\u793a\u4f8b\u7a0b\u5e8f\u4e2d a \u7684\u7c7b\u578b\u5bf9\u8c61\u662f myclass\uff0c\u4f46 myclass \u7684\u7c7b\u578b\u5bf9\u8c61\u662f\u4ec0\u4e48\u5462\uff1f<\/p>\n<p>\u6362\u53e5\u8bdd\u8bf4\uff0c\u4e00\u4e2a\u666e\u901a\u7684\u5bf9\u8c61\u7684\u7c7b\u578b\uff0c\u662f\u4e00\u4e2a\u7c7b\u578b\u5bf9\u8c61\u3002<strong>\u90a3\u4e48\u4e00\u4e2a\u7c7b\u578b\u5bf9\u8c61\u7684\u7c7b\u578b\u53c8\u662f\u4ec0\u4e48\u5462\uff1f<\/strong><\/p>\n<p>\u7b54\u6848\u662f<strong>\u5143\u7c7b\uff08metaclass\uff09<\/strong>\uff0c\u5143\u7c7b\u662f\u7c7b\u578b\u7684\u7c7b\u578b\u3002\u4e3e\u4f8b\u6765\u8bf4\uff0c\u6574\u578b\u7684 metaclass \u662f PyType_Type\u3002\u5176\u5b9e\uff0c\u5927\u90e8\u5206\u7c7b\u578b\u7684 metaclass \u662f PyType_Type\u3002<\/p>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20230829143959259.png\" alt=\"image-20230829143959259\" \/><\/p>\n<p>\u6240\u4ee5\u8bf4\uff0c\u8c03\u7528\u7c7b\u578b\u6765\u5b9e\u4f8b\u5316\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5c31\u662f\u8c03\u7528 PyType_Type \u7684 tp_call \u51fd\u6570\u3002<strong>\u90a3\u4e48 PyType_Type \u7684 tp_call \u51fd\u6570\u90fd\u505a\u4e86\u4e9b\u4ec0\u4e48\u4e8b\u60c5\u5462\uff1f<\/strong><\/p>\n<p>\u8fd9\u4e2a\u51fd\u6570\u662f type_call()\uff0c\u5b83\u4e5f\u662f\u5728 typeobject.c \u4e2d\u5b9a\u4e49\u7684\u3002Python \u4ee5 type_call() \u4e3a\u5165\u53e3\uff0c\u4f1a\u5b8c\u6210\u521b\u5efa\u4e00\u4e2a\u5bf9\u8c61\u7684\u8fc7\u7a0b\uff1a<\/p>\n<p><strong>\u521b\u5efa<\/strong><\/p>\n<p>tp_call \u4f1a\u8c03\u7528\u7c7b\u578b\u5bf9\u8c61\u7684 tp_new \u63d2\u69fd\u7684\u51fd\u6570\u3002\u5bf9\u4e8e PyLong_Type \u6765\u8bf4\uff0c\u5b83\u662f long_new\u3002<\/p>\n<p>\u5982\u679c\u6211\u4eec\u662f\u521b\u5efa\u4e00\u4e2a Point \u5bf9\u8c61\uff0c\u5982\u679c\u4f60\u4e3a\u5b83\u5b9a\u4e49\u4e86\u4e00\u4e2a\u201c<strong>new<\/strong>\u201d\u51fd\u6570\uff0c\u90a3\u4e48\u5c31\u5c06\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\u6765\u521b\u5efa\u5bf9\u8c61\uff0c\u5426\u5219\uff0c\u5c31\u4f1a\u67e5\u627e\u57fa\u7c7b\u4e2d\u7684 tp_new\u3002<\/p>\n<p><strong>\u521d\u59cb\u5316<\/strong><\/p>\n<p>tp_call \u4f1a\u8c03\u7528\u7c7b\u578b\u5bf9\u8c61\u7684 tp_init\u3002\u5bf9\u4e8e Point \u8fd9\u6837\u7684\u81ea\u5b9a\u4e49\u7c7b\u578b\u6765\u8bf4\uff0c\u5982\u679c\u5b9a\u4e49\u4e86\u201c<strong>init<\/strong>\u201d\u51fd\u6570\uff0c\u5c31\u4f1a\u6267\u884c\u6765\u505a\u521d\u59cb\u5316\u3002\u5426\u5219\uff0c\u5c31\u4f1a\u8c03\u7528\u57fa\u7c7b\u7684 tp_init\u3002\u5bf9\u4e8e PyBaseType_Object \u6765\u8bf4\uff0c\u8fd9\u4e2a\u51fd\u6570\u662f object_init\u3002<\/p>\n<p>\u9664\u4e86\u81ea\u5b9a\u4e49\u7684\u7c7b\u578b\uff0c\u5185\u7f6e\u7c7b\u578b\u7684\u5bf9\u8c61\u4e5f\u53ef\u4ee5\u7528\u7c7b\u578b\u540d\u79f0\u52a0\u62ec\u53f7\u7684\u65b9\u5f0f\u6765\u521b\u5efa\u3002\u6211\u8fd8\u662f\u4ee5\u6574\u578b\u4e3a\u4f8b\uff0c\u521b\u5efa\u4e00\u4e2a\u6574\u578b\u5bf9\u8c61\uff0c\u4e5f\u53ef\u4ee5\u7528\u201cint(10)\u201d\u8fd9\u79cd\u683c\u5f0f\uff0c\u5176\u4e2d int \u662f\u7c7b\u578b\u540d\u79f0\u3002\u800c\u4e14\uff0c\u5b83\u7684 metaclass \u4e5f\u662f PyType_Type\u3002<\/p>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20230829144013669.png\" alt=\"image-20230829144013669\" \/><\/p>\n<p>\u5f53\u7136\uff0c\u4f60\u4e5f\u53ef\u4ee5\u7ed9\u4f60\u7684\u7c7b\u578b\u6307\u5b9a\u53e6\u4e00\u4e2a metaclass\uff0c\u4ece\u800c\u652f\u6301\u4e0d\u540c\u7684\u5bf9\u8c61\u521b\u5efa\u548c\u521d\u59cb\u5316\u529f\u80fd\u3002\u867d\u7136\u5927\u90e8\u5206\u60c5\u51b5\u4e0b\u4f60\u4e0d\u9700\u8981\u8fd9\u4e48\u505a\uff0c\u4f46\u8fd9\u79cd\u53ef\u5b9a\u5236\u7684\u80fd\u529b\uff0c\u5c31\u4e3a\u4f60\u7f16\u5199\u67d0\u4e9b\u7279\u6b8a\u7684\u529f\u80fd\uff08\u6bd4\u5982\u5143\u7f16\u7a0b\uff09\u63d0\u4f9b\u4e86\u53ef\u80fd\u6027\u3002<\/p>\n<p>\u597d\u4e86\uff0c\u73b0\u5728\u4f60\u5df2\u7ecf\u77e5\u9053\uff0c\u7c7b\u578b\u7684\u7c7b\u578b\u662f\u5143\u7c7b\uff08metaclass\uff09\uff0c\u5b83\u80fd\u4e3a\u7c7b\u578b\u7684\u8c03\u7528\u63d0\u4f9b\u652f\u6301\u3002\u4f60\u53ef\u80fd\u8fdb\u4e00\u6b65\u4f1a\u95ee\uff0c<strong>\u90a3\u4e48\u5143\u7c7b\u7684\u7c7b\u578b\u53c8\u662f\u4ec0\u4e48\u5462\uff1f\u662f\u5426\u8fd8\u6709\u5143\u5143\u7c7b\uff1f\u76f4\u63a5\u8c03\u7528\u5143\u7c7b\u53c8\u4f1a\u53d1\u751f\u4ec0\u4e48\u5462\uff1f<\/strong><\/p>\n<p>\u7f3a\u7701\u60c5\u51b5\u4e0b\uff0cPyType_Type \u7684\u7c7b\u578b\u4ecd\u7136\u662f PyType_Type\uff0c\u4e5f\u5c31\u662f\u6307\u5411\u5b83\u81ea\u8eab\u3002\u5bf9\u5143\u7c7b\u505a\u8c03\u7528\uff0c\u4e5f\u4e00\u6837\u4f1a\u542f\u52a8\u4e0a\u9762\u7684 tp_call() \u8fc7\u7a0b\u3002<\/p>\n<p>\u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u6211\u4eec\u8c08\u8bba Python \u4e2d\u7684\u5bf9\u8c61\uff0c\u8fd8\u6ca1\u6709\u8c08\u8bba\u90a3\u4e9b\u9762\u5411\u5bf9\u8c61\u7684\u4f20\u7edf\u8bdd\u9898\uff1a\u7ee7\u627f\u5566\u3001\u591a\u6001\u5566\uff0c\u7b49\u7b49\u3002\u8fd9\u4e9b\u7279\u6027\u5728 Python \u4e2d\u7684\u5b9e\u73b0\uff0c\u4ecd\u7136\u53ea\u662f\u5728\u7c7b\u578b\u5bf9\u8c61\u91cc\u8bbe\u7f6e\u4e00\u4e9b\u5b57\u6bb5\u5373\u53ef\u3002\u4f60\u53ef\u4ee5\u5728 tp_base \u91cc\u8bbe\u5b9a\u57fa\u7c7b\uff08\u7236\u7c7b\uff09\u6765\u652f\u6301\u7ee7\u627f\uff0c\u751a\u81f3\u5728 tp_bases \u4e2d\u8bbe\u7f6e\u591a\u4e2a\u57fa\u7c7b\u6765\u652f\u6301\u591a\u91cd\u7ee7\u627f\u3002\u6240\u6709\u5bf9\u8c61\u7f3a\u7701\u7684\u57fa\u7c7b\u662f object\uff0ctp_base \u6307\u5411\u7684\u662f\u4e00\u4e2a PyBaseObject_Type \u5bf9\u8c61\u3002<\/p>\n<pre><code>>&gt;&gt; int.__base__     #\u67e5\u770bint\u7c7b\u578b\u7684\u57fa\u7c7b\n&lt;class &#039;object&#039;&gt;<\/code><\/pre>\n<p>\u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u6211\u4eec\u5df2\u7ecf\u5bf9\u4e8e\u5bf9\u8c61\u7684\u7c7b\u578b\u3001\u5143\u7c7b\uff0c\u4ee5\u53ca\u5bf9\u8c61\u4e4b\u95f4\u7684\u7ee7\u627f\u5173\u7cfb\u6709\u4e86\u6bd4\u8f83\u5168\u9762\u7684\u4e86\u89e3\uff0c\u4e3a\u4e86\u65b9\u4fbf\u4f60\u91cd\u70b9\u590d\u4e60\u548c\u56de\u987e\uff0c\u6211\u628a\u5b83\u4eec\u753b\u6210\u4e86\u4e00\u5f20\u56fe\u3002<\/p>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20230829144045442.png\" alt=\"image-20230829144045442\" \/><\/p>\n<p>\u4f60\u8981\u6ce8\u610f\uff0c\u56fe\u4e2d\u6211\u7528\u4e24\u79cd\u989c\u8272\u7684\u7bad\u5934\u533a\u5206\u4e86\u4e24\u79cd\u5173\u7cfb\u3002<strong>\u4e00\u79cd\u662f\u6a59\u8272\u7bad\u5934\uff0c\u4ee3\u8868\u7684\u662f\u7c7b\u578b\u5173\u7cfb<\/strong>\uff0c\u6bd4\u5982 PyLong_Type \u662f PyLongObject \u7684\u7c7b\u578b\uff0c\u800c PyType_Type \u662f PyLong_Type \u7684\u7c7b\u578b\uff1b<strong>\u53e6\u4e00\u79cd\u662f\u9ed1\u8272\u7bad\u5934\uff0c\u4ee3\u8868\u7684\u662f\u7ee7\u627f\u5173\u7cfb<\/strong>\uff0c\u6bd4\u5982 int \u7684\u57fa\u7c7b\u662f object\uff0c\u6240\u4ee5 PyLong_Type \u7684 tp_base \u6307\u5411 PyBaseObject_Type\u3002<\/p>\n<p>\u5230\u8fd9\u91cc\uff0c\u4f60\u53ef\u80fd\u4f1a\u89c9\u5f97\u6709\u70b9\u6311\u6218\u8ba4\u77e5\u3002\u56e0\u4e3a\u901a\u5e38\u6211\u4eec\u8c08\u9762\u5411\u5bf9\u8c61\u7684\u4f53\u7cfb\u7ed3\u6784\uff0c\u53ea\u4f1a\u6d89\u53ca\u56fe\u4e2d\u7684\u7ee7\u627f\u5173\u7cfb\u7ebf\uff0c\u4e0d\u592a\u4f1a\u8003\u8651\u5176\u4e2d\u7684\u7c7b\u578b\u5173\u7cfb\u7ebf\u3002Python \u7684\u7c7b\u578b\u5173\u7cfb\uff0c\u4f53\u73b0\u4e86\u201c\u6570\u636e\u5373\u7a0b\u5e8f\u201d\u7684\u6982\u5ff5\u3002Java \u8bed\u8a00\u91cc\uff0c\u67d0\u4e2a\u7c7b\u578b\u5bf9\u5e94\u4e8e\u4e00\u4e2a class \u7684\u5b57\u8282\u7801\uff0c\u800c\u5728 Python \u91cc\uff0c\u4e00\u4e2a\u7c7b\u578b\u53ea\u662f\u4e00\u4e2a Python \u5bf9\u8c61\u800c\u5df2\u3002<\/p>\n<p>\u5e76\u4e14\uff0c\u5728 Java \u91cc\u4e5f\u4e0d\u4f1a\u6709\u5143\u7c7b\uff0c\u56e0\u4e3a\u5bf9\u8c61\u7684\u521b\u5efa\u548c\u521d\u59cb\u5316\u8fc7\u7a0b\u90fd\u662f\u8bed\u8a00\u91cc\u89c4\u5b9a\u6b7b\u7684\u3002\u800c\u5728 Python \u91cc\uff0c\u4f60\u5374\u62e5\u6709\u4e00\u5b9a\u7684\u638c\u63a7\u80fd\u529b\u3002<\/p>\n<p>\u8fd9\u4e9b\u7279\u70b9\uff0c\u90fd\u4f53\u73b0\u4e86 Python \u7c7b\u578b\u4f53\u7cfb\u7684\u5f3a\u5927\u4e4b\u5904\u3002<\/p>\n<h2>\u8bfe\u7a0b\u5c0f\u7ed3<\/h2>\n<p>\u597d\u4e86\uff0c\u6211\u4eec\u6765\u603b\u7ed3\u4e00\u4e0b Python \u8fd0\u884c\u671f\u7684\u7279\u5f81\u3002\u4f60\u4f1a\u53d1\u73b0\uff0cPython \u7684\u8fd0\u884c\u671f\u8bbe\u8ba1\u7684\u6838\u5fc3\uff0c\u5c31\u662f <strong>PyObject \u5bf9\u8c61<\/strong>\uff0cPython \u5bf9\u8c61\u6240\u6709\u7684\u7279\u6027\u90fd\u662f\u4ece PyObject \u7684\u8bbe\u8ba1\u4e2d\u5ef6\u4f38\u51fa\u6765\u7684\uff0c\u7ed9\u4eba\u4e00\u79cd\u7cbe\u5de7\u7684\u7f8e\u611f\u3002<\/p>\n<ul>\n<li>Python \u7a0b\u5e8f\u4e2d\u7684\u7b26\u53f7\u90fd\u662f Python \u5bf9\u8c61\uff0c\u6808\u673a\u4e2d\u5b58\u7684\u4e5f\u90fd\u662f Python \u5bf9\u8c61\u6307\u9488\u3002<\/li>\n<li>\u6240\u6709\u5bf9\u8c61\u7684\u5934\u90e8\u4fe1\u606f\u662f\u76f8\u540c\u7684\uff0c\u800c\u540e\u9762\u7684\u4fe1\u606f\u53ef\u6269\u5c55\u3002\u8fd9\u5c31\u8ba9 Python \u53ef\u4ee5\u7528 PyObject \u6307\u9488\u6765\u8bbf\u95ee\u5404\u79cd\u5bf9\u8c61\uff0c\u8fd9\u79cd\u8bbe\u8ba1\u6280\u5de7\u4f60\u9700\u8981\u638c\u63e1\u3002<\/li>\n<li>\u6bcf\u4e2a\u5bf9\u8c61\u90fd\u6709\u7c7b\u578b\uff0c\u7c7b\u578b\u63cf\u8ff0\u4fe1\u606f\u5728\u4e00\u4e2a\u7c7b\u578b\u5bf9\u8c61\u91cc\u3002\u7cfb\u7edf\u5185\u6709\u5185\u7f6e\u7684\u7c7b\u578b\u5bf9\u8c61\uff0c\u4f60\u4e5f\u53ef\u4ee5\u901a\u8fc7 C \u8bed\u8a00\u6216 Python \u8bed\u8a00\u521b\u5efa\u65b0\u7684\u7c7b\u578b\u5bf9\u8c61\uff0c\u4ece\u800c\u652f\u6301\u65b0\u7684\u7c7b\u578b\u3002<\/li>\n<li>\u7c7b\u578b\u5bf9\u8c61\u91cc\u6709\u4e00\u4e9b\u5b57\u6bb5\u4fdd\u5b58\u4e86\u4e00\u4e9b\u51fd\u6570\u6307\u9488\uff0c\u7528\u4e8e\u5b8c\u6210\u6570\u503c\u8ba1\u7b97\u3001\u6bd4\u8f83\u7b49\u529f\u80fd\u3002\u8fd9\u662f Python \u6307\u5b9a\u7684\u63a5\u53e3\u534f\u8bae\uff0c\u7b26\u5408\u8fd9\u4e9b\u534f\u8bae\u7684\u7a0b\u5e8f\u53ef\u4ee5\u88ab\u65e0\u7f1d\u96c6\u6210\u5230 Python \u8bed\u8a00\u7684\u6846\u67b6\u4e2d\uff0c\u6bd4\u5982\u652f\u6301\u52a0\u51cf\u4e58\u9664\u8fd0\u7b97\u3002<\/li>\n<li>\u51fd\u6570\u7684\u8fd0\u884c\u3001\u5bf9\u8c61\u7684\u521b\u5efa\uff0c\u90fd\u6e90\u4e8e Python \u7684 Callable \u534f\u8bae\uff0c\u4e5f\u5c31\u662f\u5728\u7c7b\u578b\u5bf9\u8c61\u4e2d\u5236\u5b9a tp_call \u51fd\u6570\u3002\u9762\u5411\u5bf9\u8c61\u7684\u7279\u6027\uff0c\u4e5f\u662f\u901a\u8fc7\u5728\u7c7b\u578b\u5bf9\u8c61\u91cc\u5efa\u7acb\u4e0e\u57fa\u7c7b\u7684\u94fe\u63a5\u800c\u5b9e\u73b0\u7684\u3002<\/li>\n<\/ul>\n<p>\u6211\u7167\u4f8b\u628a\u672c\u8bb2\u7684\u91cd\u70b9\u77e5\u8bc6\uff0c\u6574\u7406\u6210\u4e86\u4e00\u5f20\u601d\u7ef4\u5bfc\u56fe\uff0c\u4f9b\u4f60\u53c2\u8003\u548c\u56de\u987e\uff1a<\/p>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/%E4%B8%8B%E8%BD%BD%20(2).jpg\" alt=\"\u4e0b\u8f7d (2)\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u524d\u9762\u4e24\u8bb2\u4e2d\uff0c\u6211\u4eec\u5df2\u7ecf\u5206\u6790\u4e86 Python \u4ece\u5f00\u59cb\u7f16\u8bd1\u5230\u751f\u6210\u5b57\u8282\u7801\u7684\u673a\u5236\u3002\u4f46\u662f\uff0c\u6211\u4eec\u5bf9 Python \u53ea\u662f\u4e86\u89e3\u4e86\u4e00\u534a\uff0c\u8fd8\u6709\u5f88\u591a\u95ee\u9898\u9700\u8981\u89e3   \u2026 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0},"categories":[194],"tags":[],"_links":{"self":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts\/8872"}],"collection":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=8872"}],"version-history":[{"count":2,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/8872\/revisions"}],"predecessor-version":[{"id":8874,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/8872\/revisions\/8874"}],"wp:attachment":[{"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8872"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8872"},{"taxonomy":"post_tag","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8872"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}