{"id":1179725,"date":"2025-01-15T18:27:41","date_gmt":"2025-01-15T10:27:41","guid":{"rendered":""},"modified":"2025-01-15T18:27:43","modified_gmt":"2025-01-15T10:27:43","slug":"python%e5%a6%82%e4%bd%95%e6%9f%a5%e7%9c%8b%e5%8f%98%e9%87%8f%e5%9c%b0%e5%9d%80","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1179725.html","title":{"rendered":"python\u5982\u4f55\u67e5\u770b\u53d8\u91cf\u5730\u5740"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25114046\/680b5cab-3c80-441e-bdab-641c44171fd0.webp\" alt=\"python\u5982\u4f55\u67e5\u770b\u53d8\u91cf\u5730\u5740\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u67e5\u770b\u53d8\u91cf\u7684\u5730\u5740\u53ef\u4ee5\u901a\u8fc7\u5185\u7f6e\u51fd\u6570 <code>id()<\/code> \u6765\u5b9e\u73b0\u3001<code>id()<\/code> \u51fd\u6570\u8fd4\u56de\u7684\u662f\u53d8\u91cf\u5728\u5185\u5b58\u4e2d\u7684\u5730\u5740\u3002 \u4f8b\u5982\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = 10<\/p>\n<p>print(id(a))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong><code>id(a)<\/code> \u5c06\u8fd4\u56de\u53d8\u91cf <code>a<\/code> \u7684\u5185\u5b58\u5730\u5740\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u8be6\u7ec6\u89e3\u91ca <code>id()<\/code> \u51fd\u6570\u4e4b\u524d\uff0c\u9700\u8981\u4e86\u89e3\u4e00\u4e9bPython\u5185\u5b58\u7ba1\u7406\u7684\u57fa\u672c\u6982\u5ff5\u3002Python\u7684\u5185\u5b58\u7ba1\u7406\u5668\u8d1f\u8d23\u4e3a\u53d8\u91cf\u5206\u914d\u5185\u5b58\uff0c\u5e76\u5728\u53d8\u91cf\u4e0d\u518d\u9700\u8981\u65f6\u56de\u6536\u5185\u5b58\u3002\u5728Python\u4e2d\uff0c\u53d8\u91cf\u5b9e\u9645\u4e0a\u662f\u5bf9\u5bf9\u8c61\u7684\u5f15\u7528\u3002\u6bcf\u4e2a\u5bf9\u8c61\u90fd\u6709\u4e00\u4e2a\u552f\u4e00\u7684\u6807\u8bc6\u7b26\uff0c\u8fd9\u4e2a\u6807\u8bc6\u7b26\u5728\u5bf9\u8c61\u7684\u751f\u547d\u5468\u671f\u5185\u662f\u552f\u4e00\u4e14\u4e0d\u53d8\u7684\u3002 <code>id()<\/code> \u51fd\u6570\u8fd4\u56de\u7684\u5c31\u662f\u8fd9\u4e2a\u6807\u8bc6\u7b26\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001Python\u5185\u5b58\u7ba1\u7406<\/h3>\n<\/p>\n<p><p>Python\u7684\u5185\u5b58\u7ba1\u7406\u7cfb\u7edf\u5305\u62ec\u4ee5\u4e0b\u51e0\u4e2a\u90e8\u5206\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u5f15\u7528\u8ba1\u6570<\/strong>\uff1aPython\u4f7f\u7528\u5f15\u7528\u8ba1\u6570\u6765\u8ddf\u8e2a\u5bf9\u8c61\u7684\u4f7f\u7528\u60c5\u51b5\u3002\u6bcf\u5f53\u4e00\u4e2a\u5bf9\u8c61\u88ab\u5f15\u7528\u65f6\uff0c\u5176\u5f15\u7528\u8ba1\u6570\u5c31\u4f1a\u589e\u52a0\uff1b\u5f53\u5f15\u7528\u88ab\u5220\u9664\u65f6\uff0c\u5f15\u7528\u8ba1\u6570\u51cf\u5c11\u3002\u5f53\u5f15\u7528\u8ba1\u6570\u964d\u4e3a\u96f6\u65f6\uff0c\u5185\u5b58\u7ba1\u7406\u5668\u4f1a\u81ea\u52a8\u56de\u6536\u8be5\u5bf9\u8c61\u7684\u5185\u5b58\u3002<\/li>\n<li><strong>\u5783\u573e\u56de\u6536<\/strong>\uff1a\u9664\u4e86\u5f15\u7528\u8ba1\u6570\uff0cPython\u8fd8\u4f7f\u7528\u5783\u573e\u56de\u6536\u673a\u5236\u6765\u5904\u7406\u5faa\u73af\u5f15\u7528\u3002\u5faa\u73af\u5f15\u7528\u662f\u6307\u5bf9\u8c61\u4e4b\u95f4\u76f8\u4e92\u5f15\u7528\uff0c\u5bfc\u81f4\u5f15\u7528\u8ba1\u6570\u65e0\u6cd5\u964d\u4e3a\u96f6\u7684\u60c5\u51b5\u3002Python\u7684\u5783\u573e\u56de\u6536\u5668\u4f1a\u5b9a\u671f\u68c0\u67e5\u5e76\u6e05\u9664\u8fd9\u4e9b\u5faa\u73af\u5f15\u7528\u3002<\/li>\n<li><strong>\u5185\u5b58\u6c60<\/strong>\uff1a\u4e3a\u4e86\u63d0\u9ad8\u5185\u5b58\u5206\u914d\u548c\u91ca\u653e\u7684\u6548\u7387\uff0cPython\u4f7f\u7528\u5185\u5b58\u6c60\u6280\u672f\u3002\u5185\u5b58\u6c60\u5c06\u5c0f\u5757\u5185\u5b58\u9884\u5148\u5206\u914d\u597d\uff0c\u907f\u514d\u9891\u7e41\u7684\u5185\u5b58\u5206\u914d\u548c\u91ca\u653e\u64cd\u4f5c\u3002<\/li>\n<\/ol>\n<p><h3>\u4e8c\u3001<code>id()<\/code> \u51fd\u6570\u7684\u4f7f\u7528<\/h3>\n<\/p>\n<p><p><code>id()<\/code> \u51fd\u6570\u8fd4\u56de\u5bf9\u8c61\u7684\u552f\u4e00\u6807\u8bc6\u7b26\uff0c\u8fd9\u4e2a\u6807\u8bc6\u7b26\u901a\u5e38\u662f\u5bf9\u8c61\u5728\u5185\u5b58\u4e2d\u7684\u5730\u5740\u3002\u5728CPython\uff08Python\u7684\u4e00\u79cd\u5b9e\u73b0\uff09\u4e2d\uff0c<code>id()<\/code> \u51fd\u6570\u8fd4\u56de\u7684\u662f\u5bf9\u8c61\u7684\u5185\u5b58\u5730\u5740\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = 10<\/p>\n<p>print(id(a))  # \u8f93\u51fa\u53d8\u91cfa\u7684\u5185\u5b58\u5730\u5740<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>id(a)<\/code> \u8fd4\u56de\u53d8\u91cf <code>a<\/code> \u7684\u5185\u5b58\u5730\u5740\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5bf9\u4e8e\u76f8\u540c\u7684\u6574\u6570\u503c\uff0cPython\u4f1a\u8fdb\u884c\u5bf9\u8c61\u7f13\u5b58\uff08interning\uff09\uff0c\u5373\u76f8\u540c\u7684\u6574\u6570\u503c\u4f1a\u5171\u4eab\u540c\u4e00\u4e2a\u5bf9\u8c61\u3002\u56e0\u6b64\uff0c\u4ee5\u4e0b\u4ee3\u7801\u4e2d\u7684\u4e24\u4e2a\u53d8\u91cf <code>a<\/code> \u548c <code>b<\/code> \u4f1a\u6709\u76f8\u540c\u7684\u5185\u5b58\u5730\u5740\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = 10<\/p>\n<p>b = 10<\/p>\n<p>print(id(a))  # \u8f93\u51fa\u53d8\u91cfa\u7684\u5185\u5b58\u5730\u5740<\/p>\n<p>print(id(b))  # \u8f93\u51fa\u53d8\u91cfb\u7684\u5185\u5b58\u5730\u5740<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u53d8\u91cf\u5730\u5740\u7684\u5b9e\u9645\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u67e5\u770b\u53d8\u91cf\u5730\u5740\u5728\u8c03\u8bd5\u548c\u6027\u80fd\u4f18\u5316\u4e2d\u6709\u4e00\u5b9a\u7684\u5e94\u7528\u4ef7\u503c\u3002\u4f8b\u5982\uff0c\u5728\u8c03\u8bd5\u4ee3\u7801\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u53d8\u91cf\u5730\u5740\u6765\u5224\u65ad\u4e24\u4e2a\u53d8\u91cf\u662f\u5426\u5f15\u7528\u540c\u4e00\u4e2a\u5bf9\u8c61\uff1b\u5728\u6027\u80fd\u4f18\u5316\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5206\u6790\u5bf9\u8c61\u7684\u5f15\u7528\u5173\u7cfb\u6765\u4f18\u5316\u5185\u5b58\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h4>\u8c03\u8bd5\u793a\u4f8b<\/h4>\n<\/p>\n<p><p>\u5728\u8c03\u8bd5\u4ee3\u7801\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u53d8\u91cf\u5730\u5740\u6765\u5224\u65ad\u4e24\u4e2a\u53d8\u91cf\u662f\u5426\u5f15\u7528\u540c\u4e00\u4e2a\u5bf9\u8c61\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = [1, 2, 3]<\/p>\n<p>b = a<\/p>\n<p>print(id(a))  # \u8f93\u51fa\u53d8\u91cfa\u7684\u5185\u5b58\u5730\u5740<\/p>\n<p>print(id(b))  # \u8f93\u51fa\u53d8\u91cfb\u7684\u5185\u5b58\u5730\u5740<\/p>\n<p>print(a is b)  # \u8f93\u51faTrue\uff0c\u8868\u793aa\u548cb\u5f15\u7528\u540c\u4e00\u4e2a\u5bf9\u8c61<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u53d8\u91cf <code>a<\/code> \u548c <code>b<\/code> \u5f15\u7528\u540c\u4e00\u4e2a\u5217\u8868\u5bf9\u8c61\uff0c\u56e0\u6b64\u5b83\u4eec\u7684\u5185\u5b58\u5730\u5740\u76f8\u540c\uff0c<code>a is b<\/code> \u8fd4\u56de <code>True<\/code>\u3002<\/p>\n<\/p>\n<p><h4>\u6027\u80fd\u4f18\u5316\u793a\u4f8b<\/h4>\n<\/p>\n<p><p>\u5728\u6027\u80fd\u4f18\u5316\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5206\u6790\u5bf9\u8c61\u7684\u5f15\u7528\u5173\u7cfb\u6765\u4f18\u5316\u5185\u5b58\u4f7f\u7528\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u901a\u8fc7\u51cf\u5c11\u4e0d\u5fc5\u8981\u7684\u5bf9\u8c61\u521b\u5efa\u548c\u5f15\u7528\uff0c\u6765\u964d\u4f4e\u5185\u5b58\u5360\u7528\u548c\u63d0\u9ad8\u7a0b\u5e8f\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def create_list():<\/p>\n<p>    return [1, 2, 3]<\/p>\n<p>a = create_list()<\/p>\n<p>b = create_list()<\/p>\n<p>print(id(a))  # \u8f93\u51fa\u53d8\u91cfa\u7684\u5185\u5b58\u5730\u5740<\/p>\n<p>print(id(b))  # \u8f93\u51fa\u53d8\u91cfb\u7684\u5185\u5b58\u5730\u5740<\/p>\n<p>print(a is b)  # \u8f93\u51faFalse\uff0c\u8868\u793aa\u548cb\u5f15\u7528\u4e0d\u540c\u7684\u5bf9\u8c61<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u51fd\u6570 <code>create_list<\/code> \u6bcf\u6b21\u8c03\u7528\u90fd\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5217\u8868\u5bf9\u8c61\uff0c\u56e0\u6b64\u53d8\u91cf <code>a<\/code> \u548c <code>b<\/code> \u5f15\u7528\u7684\u662f\u4e0d\u540c\u7684\u5bf9\u8c61\uff0c\u5b83\u4eec\u7684\u5185\u5b58\u5730\u5740\u4e0d\u540c\uff0c<code>a is b<\/code> \u8fd4\u56de <code>False<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u6269\u5c55\uff1aPython\u5bf9\u8c61\u7684\u751f\u547d\u5468\u671f<\/h3>\n<\/p>\n<p><p>Python\u5bf9\u8c61\u7684\u751f\u547d\u5468\u671f\u5305\u62ec\u521b\u5efa\u3001\u4f7f\u7528\u548c\u9500\u6bc1\u4e09\u4e2a\u9636\u6bb5\u3002\u5728\u5bf9\u8c61\u7684\u751f\u547d\u5468\u671f\u5185\uff0c<code>id()<\/code> \u51fd\u6570\u8fd4\u56de\u7684\u5bf9\u8c61\u6807\u8bc6\u7b26\u662f\u552f\u4e00\u4e14\u4e0d\u53d8\u7684\u3002\u5f53\u5bf9\u8c61\u88ab\u9500\u6bc1\u65f6\uff0c\u5176\u5185\u5b58\u5730\u5740\u53ef\u80fd\u4f1a\u88ab\u56de\u6536\u5e76\u91cd\u65b0\u5206\u914d\u7ed9\u5176\u4ed6\u5bf9\u8c61\u3002\u56e0\u6b64\uff0c<code>id()<\/code> \u51fd\u6570\u8fd4\u56de\u7684\u6807\u8bc6\u7b26\u5728\u5bf9\u8c61\u9500\u6bc1\u540e\u53ef\u80fd\u4f1a\u88ab\u91cd\u590d\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h4>\u5bf9\u8c61\u521b\u5efa<\/h4>\n<\/p>\n<p><p>\u5bf9\u8c61\u521b\u5efa\u662f\u6307\u5728\u5185\u5b58\u4e2d\u5206\u914d\u7a7a\u95f4\u5e76\u521d\u59cb\u5316\u5bf9\u8c61\u3002\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u53d8\u91cf\u8d4b\u503c\u3001\u51fd\u6570\u8c03\u7528\u3001\u7c7b\u5b9e\u4f8b\u5316\u7b49\u65b9\u5f0f\u521b\u5efa\u5bf9\u8c61\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = 10  # \u901a\u8fc7\u53d8\u91cf\u8d4b\u503c\u521b\u5efa\u5bf9\u8c61<\/p>\n<p>b = [1, 2, 3]  # \u901a\u8fc7\u5217\u8868\u521b\u5efa\u5bf9\u8c61<\/p>\n<p>c = {&#39;key&#39;: &#39;value&#39;}  # \u901a\u8fc7\u5b57\u5178\u521b\u5efa\u5bf9\u8c61<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e9b\u4f8b\u5b50\u4e2d\uff0c\u53d8\u91cf <code>a<\/code>\u3001<code>b<\/code> \u548c <code>c<\/code> \u5206\u522b\u5f15\u7528\u6574\u6570\u5bf9\u8c61\u3001\u5217\u8868\u5bf9\u8c61\u548c\u5b57\u5178\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><h4>\u5bf9\u8c61\u4f7f\u7528<\/h4>\n<\/p>\n<p><p>\u5bf9\u8c61\u4f7f\u7528\u662f\u6307\u5728\u7a0b\u5e8f\u4e2d\u5f15\u7528\u548c\u64cd\u4f5c\u5bf9\u8c61\u3002\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u53d8\u91cf\u5f15\u7528\u3001\u51fd\u6570\u53c2\u6570\u3001\u65b9\u6cd5\u8c03\u7528\u7b49\u65b9\u5f0f\u4f7f\u7528\u5bf9\u8c61\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = 10<\/p>\n<p>b = a + 5  # \u901a\u8fc7\u53d8\u91cf\u5f15\u7528\u4f7f\u7528\u5bf9\u8c61<\/p>\n<p>def add(x, y):<\/p>\n<p>    return x + y<\/p>\n<p>c = add(a, b)  # \u901a\u8fc7\u51fd\u6570\u53c2\u6570\u4f7f\u7528\u5bf9\u8c61<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e9b\u4f8b\u5b50\u4e2d\uff0c\u53d8\u91cf <code>a<\/code> \u548c <code>b<\/code> \u5f15\u7528\u6574\u6570\u5bf9\u8c61\uff0c\u51fd\u6570 <code>add<\/code> \u7684\u53c2\u6570 <code>x<\/code> \u548c <code>y<\/code> \u5f15\u7528\u4f20\u5165\u7684\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><h4>\u5bf9\u8c61\u9500\u6bc1<\/h4>\n<\/p>\n<p><p>\u5bf9\u8c61\u9500\u6bc1\u662f\u6307\u5728\u5185\u5b58\u4e2d\u91ca\u653e\u5bf9\u8c61\u7a7a\u95f4\u3002\u5f53\u5bf9\u8c61\u7684\u5f15\u7528\u8ba1\u6570\u964d\u4e3a\u96f6\u65f6\uff0cPython\u7684\u5185\u5b58\u7ba1\u7406\u5668\u4f1a\u81ea\u52a8\u56de\u6536\u5bf9\u8c61\u7684\u5185\u5b58\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = [1, 2, 3]<\/p>\n<p>b = a<\/p>\n<p>del a  # \u5220\u9664\u53d8\u91cfa\u7684\u5f15\u7528<\/p>\n<p>print(b)  # \u53d8\u91cfb\u4ecd\u7136\u5f15\u7528\u5217\u8868\u5bf9\u8c61<\/p>\n<p>del b  # \u5220\u9664\u53d8\u91cfb\u7684\u5f15\u7528\uff0c\u5217\u8868\u5bf9\u8c61\u7684\u5f15\u7528\u8ba1\u6570\u964d\u4e3a\u96f6\uff0c\u88ab\u9500\u6bc1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5220\u9664\u53d8\u91cf <code>a<\/code> \u7684\u5f15\u7528\u540e\uff0c\u53d8\u91cf <code>b<\/code> \u4ecd\u7136\u5f15\u7528\u5217\u8868\u5bf9\u8c61\u3002\u5f53\u53d8\u91cf <code>b<\/code> \u7684\u5f15\u7528\u4e5f\u88ab\u5220\u9664\u65f6\uff0c\u5217\u8868\u5bf9\u8c61\u7684\u5f15\u7528\u8ba1\u6570\u964d\u4e3a\u96f6\uff0c\u88ab\u9500\u6bc1\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5176\u4ed6\u67e5\u770b\u53d8\u91cf\u5730\u5740\u7684\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u9664\u4e86 <code>id()<\/code> \u51fd\u6570\uff0cPython\u8fd8\u6709\u4e00\u4e9b\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u67e5\u770b\u53d8\u91cf\u5730\u5740\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>ctypes<\/code> \u6a21\u5757\u83b7\u53d6\u53d8\u91cf\u7684\u5185\u5b58\u5730\u5740\u3002<code>ctypes<\/code> \u662fPython\u7684\u4e00\u4e2a\u5916\u90e8\u5e93\uff0c\u63d0\u4f9b\u4e86\u4e0eC\u8bed\u8a00\u517c\u5bb9\u7684\u6570\u636e\u7c7b\u578b\u548c\u51fd\u6570\u8c03\u7528\u63a5\u53e3\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import ctypes<\/p>\n<p>a = 10<\/p>\n<p>address = id(a)<\/p>\n<p>print(address)  # \u8f93\u51fa\u53d8\u91cfa\u7684\u5185\u5b58\u5730\u5740<\/p>\n<h2><strong>\u4f7f\u7528ctypes\u83b7\u53d6\u53d8\u91cf\u7684\u5185\u5b58\u5730\u5740<\/strong><\/h2>\n<p>pointer = ctypes.cast(address, ctypes.POINTER(ctypes.c_int))<\/p>\n<p>print(pointer.contents)  # \u8f93\u51fa\u53d8\u91cfa\u7684\u503c<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u9996\u5148\u4f7f\u7528 <code>id()<\/code> \u51fd\u6570\u83b7\u53d6\u53d8\u91cf <code>a<\/code> \u7684\u5185\u5b58\u5730\u5740\uff0c\u7136\u540e\u4f7f\u7528 <code>ctypes.cast<\/code> \u5c06\u5730\u5740\u8f6c\u6362\u4e3a\u6307\u5411\u6574\u6570\u7684\u6307\u9488\uff0c\u6700\u540e\u901a\u8fc7 <code>pointer.contents<\/code> \u8f93\u51fa\u6307\u9488\u6307\u5411\u7684\u6574\u6570\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5e38\u89c1\u95ee\u9898\u548c\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528 <code>id()<\/code> \u51fd\u6570\u67e5\u770b\u53d8\u91cf\u5730\u5740\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u51e0\u4e2a\u95ee\u9898\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u5bf9\u8c61\u7f13\u5b58<\/strong>\uff1aPython\u5bf9\u4e00\u4e9b\u5e38\u89c1\u7684\u5bf9\u8c61\uff08\u5982\u5c0f\u6574\u6570\u548c\u77ed\u5b57\u7b26\u4e32\uff09\u8fdb\u884c\u7f13\u5b58\uff0c\u591a\u4e2a\u53d8\u91cf\u53ef\u80fd\u5f15\u7528\u540c\u4e00\u4e2a\u5bf9\u8c61\u3002\u56e0\u6b64\uff0c\u4e0d\u540c\u53d8\u91cf\u7684\u5185\u5b58\u5730\u5740\u53ef\u80fd\u76f8\u540c\u3002\u4f8b\u5982\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">a = 10<\/p>\n<p>b = 10<\/p>\n<p>print(id(a))  # \u8f93\u51fa\u53d8\u91cfa\u7684\u5185\u5b58\u5730\u5740<\/p>\n<p>print(id(b))  # \u8f93\u51fa\u53d8\u91cfb\u7684\u5185\u5b58\u5730\u5740<\/p>\n<p>print(a is b)  # \u8f93\u51faTrue\uff0c\u8868\u793aa\u548cb\u5f15\u7528\u540c\u4e00\u4e2a\u5bf9\u8c61<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u5bf9\u8c61\u9500\u6bc1\u548c\u91cd\u7528<\/strong>\uff1a\u5bf9\u8c61\u9500\u6bc1\u540e\uff0c\u5176\u5185\u5b58\u5730\u5740\u53ef\u80fd\u4f1a\u88ab\u56de\u6536\u5e76\u91cd\u65b0\u5206\u914d\u7ed9\u5176\u4ed6\u5bf9\u8c61\u3002\u56e0\u6b64\uff0c<code>id()<\/code> \u51fd\u6570\u8fd4\u56de\u7684\u5730\u5740\u5728\u5bf9\u8c61\u9500\u6bc1\u540e\u53ef\u80fd\u4f1a\u88ab\u91cd\u590d\u4f7f\u7528\u3002\u4f8b\u5982\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">a = [1, 2, 3]<\/p>\n<p>address_a = id(a)<\/p>\n<p>del a  # \u5220\u9664\u53d8\u91cfa\u7684\u5f15\u7528\uff0c\u5217\u8868\u5bf9\u8c61\u88ab\u9500\u6bc1<\/p>\n<p>b = [4, 5, 6]<\/p>\n<p>address_b = id(b)<\/p>\n<p>print(address_a == address_b)  # \u8f93\u51faTrue\uff0c\u8868\u793a\u5730\u5740\u88ab\u91cd\u590d\u4f7f\u7528<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u4e0d\u540cPython\u5b9e\u73b0<\/strong>\uff1a\u4e0d\u540c\u7684Python\u5b9e\u73b0\uff08\u5982CPython\u3001PyPy\u3001Jython\u7b49\uff09\u53ef\u80fd\u6709\u4e0d\u540c\u7684\u5185\u5b58\u7ba1\u7406\u7b56\u7565\uff0c\u56e0\u6b64 <code>id()<\/code> \u51fd\u6570\u8fd4\u56de\u7684\u5730\u5740\u53ef\u80fd\u6709\u6240\u4e0d\u540c\u3002\u5728CPython\u4e2d\uff0c<code>id()<\/code> \u51fd\u6570\u8fd4\u56de\u7684\u662f\u5bf9\u8c61\u7684\u5185\u5b58\u5730\u5740\uff0c\u800c\u5728\u5176\u4ed6\u5b9e\u73b0\u4e2d\uff0c\u53ef\u80fd\u8fd4\u56de\u5176\u4ed6\u5f62\u5f0f\u7684\u6807\u8bc6\u7b26\u3002<\/li>\n<\/ol>\n<p><h3>\u4e03\u3001\u53d8\u91cf\u5730\u5740\u5728\u5b9e\u6218\u4e2d\u7684\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u67e5\u770b\u53d8\u91cf\u5730\u5740\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u7406\u89e3\u7a0b\u5e8f\u7684\u6267\u884c\u8fc7\u7a0b\uff0c\u5b9a\u4f4d\u548c\u89e3\u51b3\u95ee\u9898\u3002\u4ee5\u4e0b\u662f\u51e0\u4e2a\u5b9e\u9645\u5e94\u7528\u573a\u666f\uff1a<\/p>\n<\/p>\n<p><h4>1. \u5185\u5b58\u6cc4\u6f0f\u68c0\u6d4b<\/h4>\n<\/p>\n<p><p>\u5728\u4e00\u4e9b\u590d\u6742\u7684\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u5185\u5b58\u6cc4\u6f0f\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u95ee\u9898\u3002\u5185\u5b58\u6cc4\u6f0f\u662f\u6307\u7a0b\u5e8f\u5728\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u672a\u80fd\u6b63\u786e\u91ca\u653e\u4e0d\u518d\u9700\u8981\u7684\u5185\u5b58\uff0c\u5bfc\u81f4\u5185\u5b58\u5360\u7528\u4e0d\u65ad\u589e\u52a0\u3002\u901a\u8fc7\u67e5\u770b\u53d8\u91cf\u5730\u5740\u548c\u5f15\u7528\u8ba1\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u68c0\u6d4b\u548c\u5b9a\u4f4d\u5185\u5b58\u6cc4\u6f0f\u95ee\u9898\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import gc<\/p>\n<p>class MyClass:<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.data = [0] * 1024<\/p>\n<p>def create_objects():<\/p>\n<p>    objs = []<\/p>\n<p>    for _ in range(100):<\/p>\n<p>        obj = MyClass()<\/p>\n<p>        objs.append(obj)<\/p>\n<p>    return objs<\/p>\n<p>objs = create_objects()<\/p>\n<p>print(len(gc.get_objects()))  # \u8f93\u51fa\u5f53\u524d\u5185\u5b58\u4e2d\u7684\u5bf9\u8c61\u6570\u91cf<\/p>\n<p>del objs  # \u5220\u9664\u5bf9\u8c61\u5f15\u7528<\/p>\n<p>gc.collect()  # \u624b\u52a8\u89e6\u53d1\u5783\u573e\u56de\u6536<\/p>\n<p>print(len(gc.get_objects()))  # \u8f93\u51fa\u5f53\u524d\u5185\u5b58\u4e2d\u7684\u5bf9\u8c61\u6570\u91cf<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u901a\u8fc7\u67e5\u770b\u5185\u5b58\u4e2d\u7684\u5bf9\u8c61\u6570\u91cf\uff0c\u6211\u4eec\u53ef\u4ee5\u68c0\u6d4b\u548c\u5b9a\u4f4d\u5185\u5b58\u6cc4\u6f0f\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h4>2. \u6027\u80fd\u4f18\u5316<\/h4>\n<\/p>\n<p><p>\u5728\u6027\u80fd\u4f18\u5316\u4e2d\uff0c\u67e5\u770b\u53d8\u91cf\u5730\u5740\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5206\u6790\u5bf9\u8c61\u7684\u5f15\u7528\u5173\u7cfb\uff0c\u4f18\u5316\u5185\u5b58\u4f7f\u7528\u3002\u4f8b\u5982\uff0c\u5728\u4e00\u4e9b\u9ad8\u6027\u80fd\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u51cf\u5c11\u4e0d\u5fc5\u8981\u7684\u5bf9\u8c61\u521b\u5efa\u548c\u5f15\u7528\uff0c\u964d\u4f4e\u5185\u5b58\u5360\u7528\u548c\u63d0\u9ad8\u7a0b\u5e8f\u6027\u80fd\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>def create_large_list():<\/p>\n<p>    return [0] * 1000000<\/p>\n<p>start_time = time.time()<\/p>\n<p>for _ in range(100):<\/p>\n<p>    lst = create_large_list()<\/p>\n<p>print(&quot;Time taken:&quot;, time.time() - start_time)<\/p>\n<h2><strong>\u4f18\u5316\u540e<\/strong><\/h2>\n<p>start_time = time.time()<\/p>\n<p>large_list = create_large_list()<\/p>\n<p>for _ in range(100):<\/p>\n<p>    lst = large_list  # \u4f7f\u7528\u540c\u4e00\u4e2a\u5bf9\u8c61\uff0c\u907f\u514d\u91cd\u590d\u521b\u5efa<\/p>\n<p>print(&quot;Time taken:&quot;, time.time() - start_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u901a\u8fc7\u4f18\u5316\u5bf9\u8c61\u521b\u5efa\u548c\u5f15\u7528\uff0c\u663e\u8457\u51cf\u5c11\u4e86\u7a0b\u5e8f\u7684\u8fd0\u884c\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7 <code>id()<\/code> \u51fd\u6570\u67e5\u770b\u53d8\u91cf\u7684\u5730\u5740\u3002<code>id()<\/code> \u51fd\u6570\u8fd4\u56de\u7684\u662f\u5bf9\u8c61\u7684\u552f\u4e00\u6807\u8bc6\u7b26\uff0c\u901a\u5e38\u662f\u5bf9\u8c61\u5728\u5185\u5b58\u4e2d\u7684\u5730\u5740\u3002\u4e86\u89e3Python\u7684\u5185\u5b58\u7ba1\u7406\u673a\u5236\u548c\u5bf9\u8c61\u751f\u547d\u5468\u671f\uff0c\u6709\u52a9\u4e8e\u66f4\u597d\u5730\u4f7f\u7528 <code>id()<\/code> \u51fd\u6570\u3002<\/p>\n<\/p>\n<p><p>\u67e5\u770b\u53d8\u91cf\u5730\u5740\u5728\u8c03\u8bd5\u548c\u6027\u80fd\u4f18\u5316\u4e2d\u6709\u4e00\u5b9a\u7684\u5e94\u7528\u4ef7\u503c\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5206\u6790\u5bf9\u8c61\u7684\u5f15\u7528\u5173\u7cfb\uff0c\u68c0\u6d4b\u548c\u5b9a\u4f4d\u95ee\u9898\u3002\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u53ef\u4ee5\u7ed3\u5408\u5176\u4ed6\u5de5\u5177\u548c\u65b9\u6cd5\uff0c\u5982 <code>ctypes<\/code> \u6a21\u5757\u548c\u5783\u573e\u56de\u6536\u673a\u5236\uff0c\u66f4\u597d\u5730\u7ba1\u7406\u5185\u5b58\u548c\u4f18\u5316\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u4e0d\u540c\u7684Python\u5b9e\u73b0\u53ef\u80fd\u6709\u4e0d\u540c\u7684\u5185\u5b58\u7ba1\u7406\u7b56\u7565\uff0c\u56e0\u6b64\u5728\u4e0d\u540c\u7684\u5b9e\u73b0\u4e2d\uff0c<code>id()<\/code> \u51fd\u6570\u8fd4\u56de\u7684\u5730\u5740\u53ef\u80fd\u6709\u6240\u4e0d\u540c\u3002\u5728\u4f7f\u7528 <code>id()<\/code> \u51fd\u6570\u65f6\uff0c\u5e94\u6839\u636e\u5177\u4f53\u60c5\u51b5\u8fdb\u884c\u5206\u6790\u548c\u5224\u65ad\u3002\u901a\u8fc7\u5408\u7406\u4f7f\u7528 <code>id()<\/code> \u51fd\u6570\u548c\u5176\u4ed6\u5de5\u5177\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f18\u5316Python\u7a0b\u5e8f\u7684\u5185\u5b58\u7ba1\u7406\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u53ef\u9760\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u67e5\u770b\u4e00\u4e2a\u53d8\u91cf\u7684\u5185\u5b58\u5730\u5740\uff1f<\/strong><br \/>\u8981\u67e5\u770b\u4e00\u4e2a\u53d8\u91cf\u7684\u5185\u5b58\u5730\u5740\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>id()<\/code>\u51fd\u6570\u3002\u8fd9\u4e2a\u51fd\u6570\u4f1a\u8fd4\u56de\u4e00\u4e2a\u6574\u6570\uff0c\u8868\u793a\u5bf9\u8c61\u7684\u5185\u5b58\u5730\u5740\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>id(variable)<\/code>\u5373\u53ef\u83b7\u5f97\u8be5\u53d8\u91cf\u7684\u5185\u5b58\u5730\u5740\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cPython\u4e2d\u7684\u5bf9\u8c61\u53ef\u80fd\u4f1a\u5728\u4e0d\u540c\u7684\u65f6\u95f4\u70b9\u88ab\u79fb\u52a8\uff0c\u56e0\u6b64\u76f8\u540c\u7684\u53d8\u91cf\u5728\u4e0d\u540c\u60c5\u51b5\u4e0b\u7684\u5730\u5740\u53ef\u80fd\u4f1a\u4e0d\u540c\u3002<\/p>\n<p><strong>Python\u4e2d\u662f\u5426\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u83b7\u53d6\u53d8\u91cf\u7684\u5730\u5740\uff1f<\/strong><br \/>\u9664\u4e86<code>id()<\/code>\u51fd\u6570\uff0c\u4f7f\u7528<code>ctypes<\/code>\u5e93\u4e2d\u7684<code>addressof()<\/code>\u65b9\u6cd5\u4e5f\u80fd\u83b7\u53d6\u53d8\u91cf\u7684\u5185\u5b58\u5730\u5740\u3002\u9996\u5148\u9700\u8981\u5c06\u53d8\u91cf\u8f6c\u6362\u4e3a<code>ctypes<\/code>\u7c7b\u578b\uff0c\u7136\u540e\u4f7f\u7528<code>addressof()<\/code>\u83b7\u53d6\u5176\u5730\u5740\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u4e0eC\u8bed\u8a00\u63a5\u53e3\u4ea4\u4e92\u7684\u573a\u666f\uff0c\u66f4\u52a0\u5e95\u5c42\u548c\u590d\u6742\u3002<\/p>\n<p><strong>\u53d8\u91cf\u7684\u5185\u5b58\u5730\u5740\u6709\u4ec0\u4e48\u5b9e\u9645\u5e94\u7528\uff1f<\/strong><br \/>\u4e86\u89e3\u53d8\u91cf\u7684\u5185\u5b58\u5730\u5740\u5728\u8c03\u8bd5\u548c\u4f18\u5316\u7a0b\u5e8f\u65f6\u975e\u5e38\u6709\u7528\u3002\u901a\u8fc7\u67e5\u770b\u5185\u5b58\u5730\u5740\uff0c\u53ef\u4ee5\u5224\u65ad\u591a\u4e2a\u53d8\u91cf\u662f\u5426\u6307\u5411\u540c\u4e00\u4e2a\u5bf9\u8c61\uff0c\u4ece\u800c\u5e2e\u52a9\u5f00\u53d1\u8005\u4f18\u5316\u5185\u5b58\u4f7f\u7528\u548c\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u6570\u636e\u62f7\u8d1d\u3002\u6b64\u5916\uff0c\u5728\u4e00\u4e9b\u9ad8\u7ea7\u7f16\u7a0b\u573a\u666f\u4e2d\uff0c\u5185\u5b58\u5730\u5740\u7684\u64cd\u4f5c\u53ef\u4ee5\u5b9e\u73b0\u66f4\u9ad8\u6548\u7684\u6570\u636e\u5904\u7406\u548c\u5b58\u50a8\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u67e5\u770b\u53d8\u91cf\u7684\u5730\u5740\u53ef\u4ee5\u901a\u8fc7\u5185\u7f6e\u51fd\u6570 id() \u6765\u5b9e\u73b0\u3001id() \u51fd\u6570\u8fd4\u56de\u7684\u662f\u53d8\u91cf\u5728\u5185\u5b58\u4e2d\u7684\u5730\u5740\u3002 [&hellip;]","protected":false},"author":3,"featured_media":1179731,"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\/1179725"}],"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=1179725"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1179725\/revisions"}],"predecessor-version":[{"id":1179732,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1179725\/revisions\/1179732"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1179731"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1179725"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1179725"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1179725"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}