{"id":985371,"date":"2024-12-27T07:36:05","date_gmt":"2024-12-26T23:36:05","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/985371.html"},"modified":"2024-12-27T07:36:08","modified_gmt":"2024-12-26T23:36:08","slug":"python%e5%a6%82%e4%bd%95%e8%8e%b7%e5%be%97%e5%af%b9%e8%b1%a1%e6%8c%87%e9%92%88","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/985371.html","title":{"rendered":"python\u5982\u4f55\u83b7\u5f97\u5bf9\u8c61\u6307\u9488"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24212552\/ffe91dea-990a-4b0e-b36b-ee4c005d206f.webp\" alt=\"python\u5982\u4f55\u83b7\u5f97\u5bf9\u8c61\u6307\u9488\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u83b7\u53d6\u5bf9\u8c61\u7684\u6307\u9488\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528<code>id()<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\u3001Python\u4e2d\u7684\u5bf9\u8c61\u5f15\u7528\u548c\u5185\u5b58\u7ba1\u7406\u65b9\u5f0f\u4f7f\u5f97\u76f4\u63a5\u64cd\u4f5c\u6307\u9488\u4e0d\u662f\u4e00\u4e2a\u5e38\u89c1\u6216\u63a8\u8350\u7684\u64cd\u4f5c\u3001\u5bf9\u4e8e\u9700\u8981\u8fdb\u884c\u5e95\u5c42\u5185\u5b58\u64cd\u4f5c\u7684\u573a\u666f\uff0c\u901a\u5e38\u4f7f\u7528\u6269\u5c55\u5e93\u5982<code>ctypes<\/code>\u6216<code>cffi<\/code>\u6765\u63d0\u4f9b\u66f4\u4f4e\u7ea7\u522b\u7684\u5185\u5b58\u63a7\u5236<\/strong>\u3002Python\u7684\u8bbe\u8ba1\u7406\u5ff5\u662f\u63d0\u4f9b\u4e00\u4e2a\u9ad8\u7ea7\u522b\u7684\u7f16\u7a0b\u73af\u5883\uff0c\u5176\u4e2d\u5185\u5b58\u7ba1\u7406\u7531\u89e3\u91ca\u5668\u81ea\u52a8\u5904\u7406\u3002\u7136\u800c\uff0c\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4e86\u89e3\u5bf9\u8c61\u5728\u5185\u5b58\u4e2d\u7684\u4f4d\u7f6e\u6216\u4e0eC\u5e93\u8fdb\u884c\u4ea4\u4e92\u65f6\uff0c\u9700\u8981\u4f7f\u7528\u5bf9\u8c61\u7684\u5185\u5b58\u5730\u5740\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001PYTHON\u4e2d<code>id()<\/code>\u51fd\u6570\u7684\u4f7f\u7528<\/h3>\n<\/p>\n<p><p><code>id()<\/code>\u51fd\u6570\u8fd4\u56de\u5bf9\u8c61\u7684\u201c\u8eab\u4efd\u201d\uff0c\u5728CPython\u4e2d\uff0c\u8fd9\u4e2a\u8eab\u4efd\u5c31\u662f\u5bf9\u8c61\u5728\u5185\u5b58\u4e2d\u7684\u5730\u5740\u3002\u901a\u8fc7\u4f7f\u7528<code>id()<\/code>\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u83b7\u5f97\u4e00\u4e2a\u5bf9\u8c61\u5728\u5185\u5b58\u4e2d\u7684\u5730\u5740\uff0c\u867d\u7136\u8fd9\u4e0d\u662f\u771f\u6b63\u610f\u4e49\u4e0a\u7684\u201c\u6307\u9488\u201d\uff0c\u4f46\u5b83\u53ef\u4ee5\u5728\u4e00\u5b9a\u7a0b\u5ea6\u4e0a\u63d0\u4f9b\u7c7b\u4f3c\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528<code>id()<\/code>\u51fd\u6570<\/h4>\n<\/p>\n<p><p><code>id()<\/code>\u51fd\u6570\u662fPython\u5185\u7f6e\u7684\u4e00\u4e2a\u51fd\u6570\uff0c\u7528\u4e8e\u8fd4\u56de\u5bf9\u8c61\u7684\u552f\u4e00\u6807\u8bc6\u7b26\u3002\u5728CPython\u5b9e\u73b0\u4e2d\uff0c\u8fd9\u4e2a\u6807\u8bc6\u7b26\u901a\u5e38\u662f\u5bf9\u8c61\u5728\u5185\u5b58\u4e2d\u7684\u5730\u5740\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u57fa\u672c\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = [1, 2, 3]<\/p>\n<p>print(id(a))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>id(a)<\/code>\u8fd4\u56de\u7684\u662f\u5217\u8868<code>a<\/code>\u5728\u5185\u5b58\u4e2d\u7684\u5730\u5740\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u8fd9\u4e2a\u5730\u5740\u5728Python\u7a0b\u5e8f\u8fd0\u884c\u671f\u95f4\u662f\u552f\u4e00\u7684\u3002<\/p>\n<\/p>\n<p><h4>2. <code>id()<\/code>\u51fd\u6570\u7684\u5c40\u9650\u6027<\/h4>\n<\/p>\n<p><p>\u867d\u7136<code>id()<\/code>\u51fd\u6570\u63d0\u4f9b\u4e86\u5bf9\u8c61\u7684\u5185\u5b58\u5730\u5740\uff0c\u4f46\u5728Python\u4e2d\uff0c\u8fd9\u4e2a\u5730\u5740\u5e76\u4e0d\u662f\u7528\u4e8e\u76f4\u63a5\u64cd\u4f5c\u5185\u5b58\u7684\u6307\u9488\u3002Python\u7684\u5185\u5b58\u7ba1\u7406\u548c\u5783\u573e\u56de\u6536\u673a\u5236\u610f\u5473\u7740\u6211\u4eec\u4e0d\u5e94\u8be5\u76f4\u63a5\u4fee\u6539\u6216\u4f9d\u8d56\u4e8e\u5bf9\u8c61\u7684\u5185\u5b58\u5730\u5740\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001CTYPES\u4e0eCFFI\u5e93\u7684\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u9700\u8981\u66f4\u5e95\u5c42\u5185\u5b58\u64cd\u4f5c\u7684\u60c5\u51b5\uff0cPython\u63d0\u4f9b\u4e86<code>ctypes<\/code>\u548c<code>cffi<\/code>\u5e93\uff0c\u8fd9\u4e24\u4e2a\u5e93\u5141\u8bb8Python\u7a0b\u5e8f\u5458\u4e0eC\u8bed\u8a00\u5e93\u8fdb\u884c\u4ea4\u4e92\uff0c\u5e76\u63d0\u4f9b\u4e86\u66f4\u76f4\u63a5\u7684\u5185\u5b58\u8bbf\u95ee\u80fd\u529b\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528<code>ctypes<\/code>\u5e93<\/h4>\n<\/p>\n<p><p><code>ctypes<\/code>\u662fPython\u7684\u4e00\u4e2a\u5916\u90e8\u5e93\uff0c\u5141\u8bb8\u8c03\u7528C\u51fd\u6570\u548c\u4f7f\u7528C\u6570\u636e\u7c7b\u578b\u3002\u901a\u8fc7<code>ctypes<\/code>\uff0c\u53ef\u4ee5\u83b7\u5f97\u548c\u64cd\u4f5cPython\u5bf9\u8c61\u7684\u5730\u5740\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import ctypes<\/p>\n<p>a = [1, 2, 3]<\/p>\n<p>address = id(a)<\/p>\n<p>ptr = ctypes.cast(address, ctypes.POINTER(ctypes.py_object))<\/p>\n<p>print(ptr.contents)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>ctypes.cast()<\/code>\u5c06\u5bf9\u8c61\u7684\u5730\u5740\u8f6c\u6362\u4e3a\u4e00\u4e2a\u6307\u5411Python\u5bf9\u8c61\u7684\u6307\u9488\u3002<code>ptr.contents<\/code>\u5141\u8bb8\u6211\u4eec\u8bbf\u95ee\u8fd9\u4e2a\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528<code>cffi<\/code>\u5e93<\/h4>\n<\/p>\n<p><p><code>cffi<\/code>\u662f\u53e6\u4e00\u4e2a\u7528\u4e8e\u4e0eC\u5e93\u4ea4\u4e92\u7684Python\u5e93\uff0c\u63d0\u4f9b\u4e86\u66f4\u7075\u6d3b\u548c\u9ad8\u7ea7\u7684\u63a5\u53e3\u3002<code>cffi<\/code>\u53ef\u4ee5\u7528\u4e8e\u521b\u5efaC\u7ed3\u6784\u4f53\u3001\u8c03\u7528C\u51fd\u6570\u4ee5\u53ca\u8bbf\u95ee\u5185\u5b58\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from cffi import FFI<\/p>\n<p>ffi = FFI()<\/p>\n<p>a = [1, 2, 3]<\/p>\n<p>address = id(a)<\/p>\n<h2><strong>\u5728cffi\u4e2d\uff0c\u901a\u5e38\u662f\u901a\u8fc7C\u63a5\u53e3\u6765\u64cd\u4f5c\u5bf9\u8c61<\/strong><\/h2>\n<p>c_obj = ffi.from_handle(address)<\/p>\n<p>print(c_obj)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001PYTHON\u5185\u5b58\u7ba1\u7406\u673a\u5236<\/h3>\n<\/p>\n<p><p>Python\u5185\u5b58\u7ba1\u7406\u662f\u7531\u5176\u5185\u90e8\u7684\u5185\u5b58\u5206\u914d\u5668\u548c\u5783\u573e\u6536\u96c6\u5668\u8d1f\u8d23\u7684\uff0c\u7406\u89e3\u8fd9\u4e9b\u673a\u5236\u6709\u52a9\u4e8e\u66f4\u597d\u5730\u638c\u63e1\u5982\u4f55\u4f7f\u7528\u5bf9\u8c61\u6307\u9488\u3002<\/p>\n<\/p>\n<p><h4>1. \u5185\u5b58\u5206\u914d\u4e0e\u5783\u573e\u56de\u6536<\/h4>\n<\/p>\n<p><p>Python\u4f7f\u7528\u5f15\u7528\u8ba1\u6570\u4f5c\u4e3a\u5176\u4e3b\u8981\u7684\u5783\u573e\u56de\u6536\u673a\u5236\u3002\u6bcf\u5f53\u4e00\u4e2a\u5bf9\u8c61\u88ab\u5f15\u7528\u65f6\uff0c\u5176\u5f15\u7528\u8ba1\u6570\u52a0\u4e00\uff1b\u5f53\u4e00\u4e2a\u5f15\u7528\u88ab\u5220\u9664\u65f6\uff0c\u5176\u5f15\u7528\u8ba1\u6570\u51cf\u4e00\u3002\u5f53\u5f15\u7528\u8ba1\u6570\u964d\u4e3a\u96f6\u65f6\uff0c\u5185\u5b58\u88ab\u91ca\u653e\u3002<\/p>\n<\/p>\n<p><p>\u6b64\u5916\uff0cPython\u8fd8\u4f7f\u7528\u5206\u4ee3\u5783\u573e\u56de\u6536\u673a\u5236\u6765\u5904\u7406\u5faa\u73af\u5f15\u7528\u7684\u95ee\u9898\u3002\u5206\u4ee3\u5783\u573e\u56de\u6536\u5668\u5c06\u5bf9\u8c61\u5206\u4e3a\u5e74\u8f7b\u4ee3\u3001\u4e2d\u5e74\u4ee3\u548c\u8001\u5e74\u4ee3\uff0c\u9891\u7e41\u68c0\u67e5\u5e74\u8f7b\u4ee3\u4e2d\u7684\u5bf9\u8c61\u4ee5\u51cf\u5c11\u5783\u573e\u56de\u6536\u7684\u5f00\u9500\u3002<\/p>\n<\/p>\n<p><h4>2. \u5185\u5b58\u7ba1\u7406\u7684\u4f18\u52bf\u4e0e\u9650\u5236<\/h4>\n<\/p>\n<p><p>Python\u7684\u5185\u5b58\u7ba1\u7406\u673a\u5236\u4f7f\u5f97\u7f16\u7a0b\u66f4\u52a0\u7b80\u5355\u548c\u5b89\u5168\uff0c\u7a0b\u5e8f\u5458\u4e0d\u5fc5\u663e\u5f0f\u5730\u91ca\u653e\u5185\u5b58\u3002\u7136\u800c\uff0c\u8fd9\u4e5f\u610f\u5473\u7740Python\u4e0d\u63d0\u4f9b\u76f4\u63a5\u7684\u6307\u9488\u64cd\u4f5c\u80fd\u529b\u3002\u5728\u67d0\u4e9b\u9700\u8981\u9ad8\u6027\u80fd\u6216\u7279\u5b9a\u5185\u5b58\u5e03\u5c40\u7684\u573a\u666f\u4e0b\uff0c\u8fd9\u53ef\u80fd\u6210\u4e3a\u4e00\u4e2a\u9650\u5236\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5bf9\u8c61\u6307\u9488\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u4f7f\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0cPython\u7a0b\u5e8f\u5458\u4e0d\u9700\u8981\u76f4\u63a5\u8bbf\u95ee\u5bf9\u8c61\u7684\u5185\u5b58\u5730\u5740\u3002\u7136\u800c\uff0c\u5728\u67d0\u4e9b\u7279\u5b9a\u7684\u573a\u666f\u4e0b\uff0c\u83b7\u53d6\u5bf9\u8c61\u5730\u5740\u53ef\u80fd\u4f1a\u53d8\u5f97\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h4>1. \u4e0eC\u8bed\u8a00\u5e93\u7684\u4ea4\u4e92<\/h4>\n<\/p>\n<p><p>\u5f53Python\u4ee3\u7801\u9700\u8981\u4e0eC\u8bed\u8a00\u5e93\u4ea4\u4e92\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u5c06Python\u5bf9\u8c61\u7684\u5730\u5740\u4f20\u9012\u7ed9C\u51fd\u6570\u3002\u4f8b\u5982\uff0c\u4f7f\u7528Python\u8c03\u7528C\u51fd\u6570\u8fdb\u884c\u56fe\u50cf\u5904\u7406\u3001\u79d1\u5b66\u8ba1\u7b97\u6216\u8005\u64cd\u4f5c\u7cfb\u7edf\u529f\u80fd\u65f6\uff0c\u9700\u8981\u83b7\u53d6\u5bf9\u8c61\u7684\u5185\u5b58\u5730\u5740\u3002<\/p>\n<\/p>\n<p><h4>2. \u8c03\u8bd5\u548c\u6027\u80fd\u5206\u6790<\/h4>\n<\/p>\n<p><p>\u5728\u8c03\u8bd5\u6216\u8005\u8fdb\u884c\u6027\u80fd\u5206\u6790\u65f6\uff0c\u4e86\u89e3\u5bf9\u8c61\u5728\u5185\u5b58\u4e2d\u7684\u4f4d\u7f6e\u53ef\u80fd\u6709\u52a9\u4e8e\u8bc6\u522b\u6f5c\u5728\u7684\u95ee\u9898\u3002\u4f8b\u5982\uff0c\u5206\u6790\u5185\u5b58\u4f7f\u7528\u6a21\u5f0f\u3001\u8bc6\u522b\u5185\u5b58\u6cc4\u6f0f\u6216\u8005\u7406\u89e3\u5bf9\u8c61\u7684\u751f\u547d\u5468\u671f\u3002<\/p>\n<\/p>\n<p><h4>3. \u7279\u6b8a\u7684\u5185\u5b58\u4f18\u5316<\/h4>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u9ad8\u7ea7\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u5bf9\u5bf9\u8c61\u5185\u5b58\u8fdb\u884c\u7279\u6b8a\u7684\u4f18\u5316\u5904\u7406\u3002\u4f8b\u5982\uff0c\u5728\u5b9e\u73b0\u81ea\u5b9a\u4e49\u7684\u5185\u5b58\u6c60\u3001\u5171\u4eab\u5185\u5b58\u5bf9\u8c61\u6216\u8005\u81ea\u5b9a\u4e49\u7684\u5e8f\u5217\u5316\u673a\u5236\u65f6\uff0c\u83b7\u53d6\u5bf9\u8c61\u7684\u5185\u5b58\u5730\u5740\u53ef\u80fd\u662f\u5fc5\u8981\u7684\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001PYTHON\u5bf9\u8c61\u6307\u9488\u7684\u6f5c\u5728\u98ce\u9669<\/h3>\n<\/p>\n<p><p>\u5c3d\u7ba1\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u83b7\u5f97\u5bf9\u8c61\u5730\u5740\u662f\u6709\u7528\u7684\uff0c\u4f46\u76f4\u63a5\u64cd\u4f5c\u5185\u5b58\u5730\u5740\u4e5f\u5e26\u6765\u4e86\u6f5c\u5728\u7684\u98ce\u9669\u3002<\/p>\n<\/p>\n<p><h4>1. \u5185\u5b58\u5b89\u5168\u95ee\u9898<\/h4>\n<\/p>\n<p><p>\u76f4\u63a5\u64cd\u4f5c\u5185\u5b58\u5730\u5740\u53ef\u80fd\u5bfc\u81f4\u5185\u5b58\u5b89\u5168\u95ee\u9898\uff0c\u4f8b\u5982\uff0c\u5185\u5b58\u6cc4\u6f0f\u3001\u975e\u6cd5\u5185\u5b58\u8bbf\u95ee\u6216\u8005\u7834\u574fPython\u7684\u5185\u5b58\u7ba1\u7406\u673a\u5236\u3002\u8fd9\u4e9b\u95ee\u9898\u53ef\u80fd\u5bfc\u81f4\u7a0b\u5e8f\u5d29\u6e83\u6216\u8005\u4ea7\u751f\u4e0d\u53ef\u9884\u6d4b\u7684\u884c\u4e3a\u3002<\/p>\n<\/p>\n<p><h4>2. \u517c\u5bb9\u6027\u95ee\u9898<\/h4>\n<\/p>\n<p><p>\u4e0d\u540cPython\u89e3\u91ca\u5668\u6216\u8005\u4e0d\u540c\u7248\u672c\u7684\u540c\u4e00\u89e3\u91ca\u5668\u53ef\u80fd\u5728\u5185\u5b58\u7ba1\u7406\u65b9\u9762\u5b58\u5728\u5dee\u5f02\u3002\u56e0\u6b64\uff0c\u4f9d\u8d56\u4e8e\u5bf9\u8c61\u5730\u5740\u7684\u4ee3\u7801\u53ef\u80fd\u5728\u4e0d\u540c\u73af\u5883\u4e0b\u8868\u73b0\u4e0d\u4e00\u81f4\uff0c\u5bfc\u81f4\u517c\u5bb9\u6027\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h4>3. \u53ef\u7ef4\u62a4\u6027\u95ee\u9898<\/h4>\n<\/p>\n<p><p>\u76f4\u63a5\u64cd\u4f5c\u5185\u5b58\u5730\u5740\u53ef\u80fd\u964d\u4f4e\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u5927\u591a\u6570Python\u7a0b\u5e8f\u5458\u4e60\u60ef\u4e8e\u4f7f\u7528Python\u7684\u9ad8\u7ea7\u7279\u6027\uff0c\u800c\u4e0d\u662f\u5e95\u5c42\u5185\u5b58\u64cd\u4f5c\uff0c\u56e0\u6b64\u8fd9\u79cd\u4ee3\u7801\u53ef\u80fd\u96be\u4ee5\u7406\u89e3\u548c\u7ef4\u62a4\u3002<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u867d\u7136\u53ef\u4ee5\u901a\u8fc7<code>id()<\/code>\u51fd\u6570\u548c\u6269\u5c55\u5e93\u5982<code>ctypes<\/code>\u6216<code>cffi<\/code>\u6765\u83b7\u53d6\u5bf9\u8c61\u7684\u5185\u5b58\u5730\u5740\uff0c\u4f46\u76f4\u63a5\u64cd\u4f5c\u6307\u9488\u5e76\u4e0d\u662f\u5e38\u89c1\u6216\u63a8\u8350\u7684\u505a\u6cd5\u3002Python\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u9ad8\u7ea7\u7279\u6027\u6765\u7b80\u5316\u7f16\u7a0b\uff0c\u800c\u5185\u5b58\u7ba1\u7406\u548c\u5783\u573e\u56de\u6536\u673a\u5236\u4f7f\u5f97\u5927\u90e8\u5206\u60c5\u51b5\u4e0b\u4e0d\u9700\u8981\u76f4\u63a5\u64cd\u4f5c\u5185\u5b58\u5730\u5740\u3002\u5728\u9700\u8981\u4e0eC\u8bed\u8a00\u5e93\u4ea4\u4e92\u6216\u8fdb\u884c\u7279\u6b8a\u5185\u5b58\u64cd\u4f5c\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e9b\u6280\u672f\uff0c\u4f46\u9700\u8981\u6ce8\u610f\u6f5c\u5728\u7684\u98ce\u9669\u548c\u517c\u5bb9\u6027\u95ee\u9898\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u83b7\u53d6\u5bf9\u8c61\u7684\u5185\u5b58\u5730\u5740\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>id()<\/code>\u51fd\u6570\u6765\u83b7\u53d6\u5bf9\u8c61\u7684\u5185\u5b58\u5730\u5740\u3002\u8be5\u51fd\u6570\u8fd4\u56de\u5bf9\u8c61\u7684\u552f\u4e00\u6807\u8bc6\u7b26\uff0c\u8fd9\u901a\u5e38\u662f\u5bf9\u8c61\u5728\u5185\u5b58\u4e2d\u7684\u5730\u5740\u3002\u4f7f\u7528\u65b9\u6cd5\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">obj = SomeClass()\nprint(id(obj))\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u83b7\u5f97<code>obj<\/code>\u5bf9\u8c61\u7684\u5185\u5b58\u5730\u5740\u3002<\/p>\n<p><strong>Python\u4e2d\u5bf9\u8c61\u7684\u5f15\u7528\u548c\u6307\u9488\u6709\u4ec0\u4e48\u533a\u522b\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u6240\u6709\u53d8\u91cf\u90fd\u662f\u5bf9\u8c61\u7684\u5f15\u7528\uff0c\u800c\u4e0d\u662f\u76f4\u63a5\u7684\u6307\u9488\u3002\u5f15\u7528\u6307\u5411\u5bf9\u8c61\uff0c\u5141\u8bb8\u901a\u8fc7\u53d8\u91cf\u8bbf\u95ee\u5bf9\u8c61\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u3002\u4e0eC\u6216C++\u7b49\u8bed\u8a00\u4e2d\u7684\u6307\u9488\u4e0d\u540c\uff0cPython\u7684\u5f15\u7528\u7ba1\u7406\u5185\u5b58\u7684\u590d\u6742\u6027\uff0c\u63d0\u4f9b\u4e86\u66f4\u9ad8\u5c42\u6b21\u7684\u62bd\u8c61\uff0c\u907f\u514d\u4e86\u76f4\u63a5\u64cd\u4f5c\u5185\u5b58\u5730\u5740\u53ef\u80fd\u5bfc\u81f4\u7684\u9519\u8bef\u3002<\/p>\n<p><strong>\u5982\u4f55\u5224\u65ad\u4e24\u4e2a\u5bf9\u8c61\u662f\u5426\u6307\u5411\u540c\u4e00\u4e2a\u5185\u5b58\u5730\u5740\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>is<\/code>\u8fd0\u7b97\u7b26\u6765\u5224\u65ad\u4e24\u4e2a\u5bf9\u8c61\u662f\u5426\u5f15\u7528\u540c\u4e00\u4e2a\u5185\u5b58\u5730\u5740\u3002\u82e5\u4e24\u4e2a\u5bf9\u8c61\u662f\u540c\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u4f7f\u7528<code>is<\/code>\u5c06\u8fd4\u56de<code>True<\/code>\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">a = [1, 2, 3]\nb = a\nprint(a is b)  # \u8f93\u51fa True\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>a<\/code>\u548c<code>b<\/code>\u90fd\u6307\u5411\u540c\u4e00\u4e2a\u5217\u8868\u5bf9\u8c61\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u83b7\u53d6\u5bf9\u8c61\u7684\u5185\u5b58\u7ba1\u7406\u7684\u6700\u4f73\u5b9e\u8df5\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>Python\u6709\u81ea\u52a8\u5783\u573e\u56de\u6536\u673a\u5236\uff0c\u901a\u5e38\u4e0d\u9700\u8981\u624b\u52a8\u7ba1\u7406\u5185\u5b58\u3002\u7136\u800c\uff0c\u4fdd\u6301\u826f\u597d\u7684\u7f16\u7a0b\u4e60\u60ef\uff0c\u6bd4\u5982\u907f\u514d\u5faa\u73af\u5f15\u7528\u3001\u4f7f\u7528\u5f31\u5f15\u7528\uff08<code>weakref<\/code>\u6a21\u5757\uff09\u7b49\uff0c\u53ef\u4ee5\u5e2e\u52a9\u63d0\u9ad8\u5185\u5b58\u7ba1\u7406\u7684\u6548\u7387\u3002\u4e86\u89e3\u548c\u4f7f\u7528\u8fd9\u4e9b\u673a\u5236\u53ef\u4ee5\u6709\u6548\u51cf\u5c11\u5185\u5b58\u6cc4\u6f0f\u548c\u63d0\u9ad8\u7a0b\u5e8f\u6027\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u83b7\u53d6\u5bf9\u8c61\u7684\u6307\u9488\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528id()\u51fd\u6570\u6765\u5b9e\u73b0\u3001Python\u4e2d\u7684\u5bf9\u8c61\u5f15\u7528\u548c\u5185\u5b58\u7ba1\u7406\u65b9\u5f0f\u4f7f\u5f97\u76f4\u63a5 [&hellip;]","protected":false},"author":3,"featured_media":985380,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/985371"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=985371"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/985371\/revisions"}],"predecessor-version":[{"id":985385,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/985371\/revisions\/985385"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/985380"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=985371"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=985371"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=985371"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}