{"id":1160867,"date":"2025-01-13T19:10:00","date_gmt":"2025-01-13T11:10:00","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1160867.html"},"modified":"2025-01-13T19:10:03","modified_gmt":"2025-01-13T11:10:03","slug":"python%e5%a6%82%e4%bd%95%e6%b1%82%e5%8f%98%e9%87%8f%e5%9c%b0%e5%9d%80","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1160867.html","title":{"rendered":"Python\u5982\u4f55\u6c42\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\/25202253\/1883f60f-b0e1-48ad-800b-90f82b971874.webp\" alt=\"Python\u5982\u4f55\u6c42\u53d8\u91cf\u5730\u5740\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u53ef\u4ee5\u901a\u8fc7\u5185\u7f6e\u51fd\u6570id()\u6765\u83b7\u53d6\u53d8\u91cf\u7684\u5730\u5740\u3001id()\u51fd\u6570\u8fd4\u56de\u5bf9\u8c61\u7684\u552f\u4e00\u6807\u8bc6\u7b26\u3001\u901a\u5e38\u662f\u5bf9\u8c61\u5728\u5185\u5b58\u4e2d\u7684\u5730\u5740\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u5c55\u5f00\u8be6\u7ec6\u63cf\u8ff0\uff1aPython\u4e2d\u7684<code>id()<\/code>\u51fd\u6570\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u5bf9\u8c61\u7684\u201c\u6807\u8bc6\u7b26\u201d\uff0c\u8fd9\u4e2a\u6807\u8bc6\u7b26\u5728CPython\u5b9e\u73b0\u4e2d\u901a\u5e38\u662f\u5bf9\u8c61\u5728\u5185\u5b58\u4e2d\u7684\u5730\u5740\uff0c\u4f46\u5728\u5176\u4ed6Python\u5b9e\u73b0\u4e2d\uff0c\u8fd9\u4e2a\u6807\u8bc6\u7b26\u53ef\u80fd\u4e0d\u662f\u5185\u5b58\u5730\u5740\uff0c\u4f46\u5b83\u5728\u5bf9\u8c61\u751f\u547d\u5468\u671f\u5185\u662f\u552f\u4e00\u7684\u3002\u4f7f\u7528<code>id()<\/code>\u51fd\u6570\u5f88\u7b80\u5355\uff0c\u53ea\u9700\u8981\u4f20\u5165\u4e00\u4e2a\u5bf9\u8c61\uff08\u53d8\u91cf\uff09\uff0c\u5c31\u53ef\u4ee5\u5f97\u5230\u5b83\u7684\u6807\u8bc6\u7b26\u3002<\/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>\u4ee5\u4e0a\u4ee3\u7801\u5c06\u8f93\u51fa\u53d8\u91cf<code>a<\/code>\u7684\u6807\u8bc6\u7b26\uff0c\u901a\u5e38\u662f\u5185\u5b58\u5730\u5740\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001PYTHON\u5185\u5b58\u7ba1\u7406<\/h3>\n<\/p>\n<p><p>Python\u6709\u4e00\u5957\u81ea\u5df1\u7684\u5185\u5b58\u7ba1\u7406\u673a\u5236\uff0c\u8d1f\u8d23\u5bf9\u8c61\u7684\u5206\u914d\u548c\u91ca\u653e\u3002\u4e86\u89e3\u8fd9\u90e8\u5206\u5185\u5bb9\u6709\u52a9\u4e8e\u66f4\u597d\u5730\u7406\u89e3\u53d8\u91cf\u5730\u5740\u7684\u6982\u5ff5\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5f15\u7528\u8ba1\u6570<\/h4>\n<\/p>\n<p><p>Python\u4f7f\u7528\u5f15\u7528\u8ba1\u6570\u6765\u7ba1\u7406\u5185\u5b58\u4e2d\u7684\u5bf9\u8c61\u3002\u5f53\u4e00\u4e2a\u5bf9\u8c61\u88ab\u521b\u5efa\u65f6\uff0c\u5b83\u7684\u5f15\u7528\u8ba1\u6570\u4e3a1\u3002\u5f53\u6709\u65b0\u7684\u5f15\u7528\u6307\u5411\u8be5\u5bf9\u8c61\u65f6\uff0c\u5f15\u7528\u8ba1\u6570\u589e\u52a0\uff1b\u5f53\u5f15\u7528\u88ab\u5220\u9664\u65f6\uff0c\u5f15\u7528\u8ba1\u6570\u51cf\u5c11\u3002\u5982\u679c\u5f15\u7528\u8ba1\u6570\u964d\u4e3a0\uff0cPython\u7684\u5783\u573e\u56de\u6536\u673a\u5236\u4f1a\u91ca\u653e\u8be5\u5bf9\u8c61\u5360\u7528\u7684\u5185\u5b58\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>a = []<\/p>\n<p>print(sys.getrefcount(a))  # \u8f93\u51fa2\uff0c\u56e0\u4e3agetrefcount()\u672c\u8eab\u4e5f\u4f1a\u521b\u5efa\u4e00\u4e2a\u5f15\u7528<\/p>\n<p>b = a<\/p>\n<p>print(sys.getrefcount(a))  # \u8f93\u51fa3\uff0c\u56e0\u4e3ab\u4e5f\u5f15\u7528\u4e86a<\/p>\n<p>del b<\/p>\n<p>print(sys.getrefcount(a))  # \u8f93\u51fa2\uff0c\u5220\u9664\u4e86b\u7684\u5f15\u7528<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5783\u573e\u56de\u6536<\/h4>\n<\/p>\n<p><p>\u9664\u4e86\u5f15\u7528\u8ba1\u6570\uff0cPython\u8fd8\u6709\u5783\u573e\u56de\u6536\u673a\u5236\u6765\u5904\u7406\u5faa\u73af\u5f15\u7528\u7684\u60c5\u51b5\u3002\u5783\u573e\u56de\u6536\u5668\u4f1a\u5468\u671f\u6027\u5730\u68c0\u67e5\u5bf9\u8c61\u56fe\uff0c\u8bc6\u522b\u548c\u6e05\u7406\u65e0\u7528\u7684\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import gc<\/p>\n<p>gc.collect()  # \u624b\u52a8\u89e6\u53d1\u5783\u573e\u56de\u6536<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001PYTHON\u7684ID\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>id()<\/code>\u51fd\u6570\u662f\u83b7\u53d6\u53d8\u91cf\u5730\u5740\u7684\u5173\u952e\u5de5\u5177\u3002\u5b83\u8fd4\u56de\u5bf9\u8c61\u7684\u6807\u8bc6\u7b26\uff0c\u8fd9\u4e2a\u6807\u8bc6\u7b26\u5728\u5bf9\u8c61\u751f\u547d\u5468\u671f\u5185\u662f\u552f\u4e00\u7684\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p><code>id()<\/code>\u51fd\u6570\u7684\u7528\u6cd5\u975e\u5e38\u7b80\u5355\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = 10<\/p>\n<p>print(id(a))  # \u8f93\u51fa\u53d8\u91cfa\u7684\u6807\u8bc6\u7b26<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4e0e\u5185\u5b58\u5730\u5740\u7684\u5173\u7cfb<\/h4>\n<\/p>\n<p><p>\u5728CPython\u5b9e\u73b0\u4e2d\uff0c<code>id()<\/code>\u51fd\u6570\u8fd4\u56de\u7684\u662f\u5bf9\u8c61\u5728\u5185\u5b58\u4e2d\u7684\u5730\u5740\uff0c\u4f46\u5728\u5176\u4ed6Python\u5b9e\u73b0\u4e2d\uff0c\u8fd9\u4e2a\u6807\u8bc6\u7b26\u53ef\u80fd\u4e0d\u662f\u5185\u5b58\u5730\u5740\u3002\u4f8b\u5982\uff0c\u5728PyPy\u4e2d\uff0c<code>id()<\/code>\u7684\u5b9e\u73b0\u548cCPython\u4e0d\u540c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import ctypes<\/p>\n<p>a = 10<\/p>\n<p>print(hex(id(a)))  # \u8f6c\u6362\u4e3a\u5341\u516d\u8fdb\u5236\u5f62\u5f0f\uff0c\u901a\u5e38\u53ef\u4ee5\u8ba4\u4e3a\u662f\u5185\u5b58\u5730\u5740<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001PYTHON\u53d8\u91cf\u7684\u5b58\u50a8<\/h3>\n<\/p>\n<p><p>Python\u53d8\u91cf\u7684\u5b58\u50a8\u673a\u5236\u51b3\u5b9a\u4e86\u4e3a\u4ec0\u4e48\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7<code>id()<\/code>\u83b7\u53d6\u53d8\u91cf\u7684\u6807\u8bc6\u7b26\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u53d8\u91cf\u4e0e\u5bf9\u8c61<\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53d8\u91cf\u662f\u5bf9\u5bf9\u8c61\u7684\u5f15\u7528\u3002\u6bcf\u4e2a\u5bf9\u8c61\u5728\u5185\u5b58\u4e2d\u90fd\u6709\u4e00\u4e2a\u552f\u4e00\u7684\u6807\u8bc6\u7b26\uff0c\u53d8\u91cf\u53ea\u662f\u6307\u5411\u8fd9\u4e2a\u5bf9\u8c61\u7684\u201c\u6807\u7b7e\u201d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = 10<\/p>\n<p>b = 10<\/p>\n<p>print(id(a) == id(b))  # \u8f93\u51faTrue\uff0c\u56e0\u4e3aa\u548cb\u6307\u5411\u540c\u4e00\u4e2a\u5bf9\u8c61<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4e0d\u53ef\u53d8\u5bf9\u8c61\u4e0e\u53ef\u53d8\u5bf9\u8c61<\/h4>\n<\/p>\n<p><p>\u4e0d\u53ef\u53d8\u5bf9\u8c61\uff08\u5982\u6574\u6570\u3001\u5b57\u7b26\u4e32\u3001\u5143\u7ec4\uff09\u5728\u521b\u5efa\u540e\u4e0d\u80fd\u4fee\u6539\uff0c\u56e0\u6b64\u76f8\u540c\u503c\u7684\u4e0d\u53ef\u53d8\u5bf9\u8c61\u53ef\u80fd\u4f1a\u5171\u4eab\u540c\u4e00\u4e2a\u5185\u5b58\u5730\u5740\u3002\u800c\u53ef\u53d8\u5bf9\u8c61\uff08\u5982\u5217\u8868\u3001\u5b57\u5178\u3001\u96c6\u5408\uff09\u6bcf\u6b21\u521b\u5efa\u90fd\u662f\u4e0d\u540c\u7684\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = (1, 2, 3)<\/p>\n<p>b = (1, 2, 3)<\/p>\n<p>print(id(a) == id(b))  # \u8f93\u51faFalse\uff0c\u56e0\u4e3a\u5143\u7ec4\u662f\u4e0d\u53ef\u53d8\u5bf9\u8c61<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001ID\u51fd\u6570\u7684\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u7406\u89e3\u5e76\u5229\u7528<code>id()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5728\u8c03\u8bd5\u548c\u4f18\u5316\u4ee3\u7801\u65f6\u66f4\u52a0\u5f97\u5fc3\u5e94\u624b\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u8c03\u8bd5\u5f15\u7528\u95ee\u9898<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u68c0\u67e5\u5bf9\u8c61\u7684\u6807\u8bc6\u7b26\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5b9a\u4f4d\u5f15\u7528\u95ee\u9898\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u590d\u6742\u6570\u636e\u7ed3\u6784\u65f6\u3002<\/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) == id(b))  # \u8f93\u51faTrue\uff0c\u56e0\u4e3aa\u548cb\u5f15\u7528\u540c\u4e00\u4e2a\u5217\u8868<\/p>\n<p>b.append(4)<\/p>\n<p>print(a)  # \u8f93\u51fa[1, 2, 3, 4]\uff0c\u56e0\u4e3aa\u548cb\u662f\u540c\u4e00\u4e2a\u5bf9\u8c61<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f18\u5316\u5185\u5b58\u4f7f\u7528<\/h4>\n<\/p>\n<p><p>\u7406\u89e3\u5bf9\u8c61\u7684\u5185\u5b58\u5730\u5740\u548c\u5f15\u7528\u5173\u7cfb\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u4f18\u5316\u5185\u5b58\u4f7f\u7528\uff0c\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u5bf9\u8c61\u521b\u5efa\u548c\u590d\u5236\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import copy<\/p>\n<p>a = [1, 2, 3]<\/p>\n<p>b = copy.deepcopy(a)<\/p>\n<p>print(id(a) == id(b))  # \u8f93\u51faFalse\uff0c\u6df1\u62f7\u8d1d\u521b\u5efa\u4e86\u65b0\u7684\u5bf9\u8c61<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001ID\u51fd\u6570\u7684\u5c40\u9650\u6027<\/h3>\n<\/p>\n<p><p>\u5c3d\u7ba1<code>id()<\/code>\u51fd\u6570\u975e\u5e38\u6709\u7528\uff0c\u4f46\u5b83\u4e5f\u6709\u4e00\u4e9b\u5c40\u9650\u6027\uff0c\u9700\u8981\u6ce8\u610f\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u8de8Python\u5b9e\u73b0\u7684\u5dee\u5f02<\/h4>\n<\/p>\n<p><p>\u4e0d\u540c\u7684Python\u5b9e\u73b0\u53ef\u80fd\u5bf9<code>id()<\/code>\u7684\u5b9e\u73b0\u4e0d\u540c\uff0c\u5728CPython\u4e2d\uff0c<code>id()<\/code>\u8fd4\u56de\u7684\u662f\u5185\u5b58\u5730\u5740\uff0c\u4f46\u5728\u5176\u4ed6\u5b9e\u73b0\u4e2d\u4e0d\u4e00\u5b9a\u662f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5728CPython\u4e2d<\/p>\n<p>print(id(10))  # \u8f93\u51fa\u67d0\u4e2a\u5185\u5b58\u5730\u5740<\/p>\n<h2><strong>\u5728PyPy\u4e2d<\/strong><\/h2>\n<h2><strong>print(id(10))  # \u53ef\u80fd\u8f93\u51fa\u4e0d\u540c\u7684\u7ed3\u679c<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5bf9\u8c61\u751f\u547d\u5468\u671f<\/h4>\n<\/p>\n<p><p><code>id()<\/code>\u8fd4\u56de\u7684\u6807\u8bc6\u7b26\u5728\u5bf9\u8c61\u751f\u547d\u5468\u671f\u5185\u662f\u552f\u4e00\u7684\uff0c\u4f46\u5bf9\u8c61\u88ab\u9500\u6bc1\u540e\uff0c\u8fd9\u4e2a\u6807\u8bc6\u7b26\u53ef\u80fd\u4f1a\u88ab\u91cd\u65b0\u5206\u914d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = 10<\/p>\n<p>print(id(a))  # \u8f93\u51fa\u67d0\u4e2a\u6807\u8bc6\u7b26<\/p>\n<p>del a<\/p>\n<p>b = 20<\/p>\n<p>print(id(b))  # \u53ef\u80fd\u8f93\u51fa\u4e0e\u4e4b\u524d\u76f8\u540c\u7684\u6807\u8bc6\u7b26<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u6df1\u5165\u7406\u89e3PYTHON\u5bf9\u8c61\u6a21\u578b<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u597d\u5730\u7406\u89e3\u53d8\u91cf\u5730\u5740\uff0c\u6df1\u5165\u7406\u89e3Python\u7684\u5bf9\u8c61\u6a21\u578b\u662f\u5f88\u6709\u5e2e\u52a9\u7684\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5bf9\u8c61\u7684\u7ec4\u6210<\/h4>\n<\/p>\n<p><p>\u6bcf\u4e2aPython\u5bf9\u8c61\u90fd\u6709\u4e09\u4e2a\u57fa\u672c\u5c5e\u6027\uff1a\u7c7b\u578b\u3001\u503c\u548c\u5f15\u7528\u8ba1\u6570\u3002\u7c7b\u578b\u51b3\u5b9a\u4e86\u5bf9\u8c61\u7684\u884c\u4e3a\u548c\u65b9\u6cd5\uff0c\u503c\u662f\u5bf9\u8c61\u5b58\u50a8\u7684\u6570\u636e\uff0c\u5f15\u7528\u8ba1\u6570\u7ba1\u7406\u5bf9\u8c61\u7684\u751f\u547d\u5468\u671f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = 10<\/p>\n<p>print(type(a))  # \u8f93\u51fa\u5bf9\u8c61\u7684\u7c7b\u578b<\/p>\n<p>print(a)  # \u8f93\u51fa\u5bf9\u8c61\u7684\u503c<\/p>\n<p>print(sys.getrefcount(a))  # \u8f93\u51fa\u5bf9\u8c61\u7684\u5f15\u7528\u8ba1\u6570<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5bf9\u8c61\u7684\u521b\u5efa\u4e0e\u9500\u6bc1<\/h4>\n<\/p>\n<p><p>\u5bf9\u8c61\u7684\u521b\u5efa\u4e0e\u9500\u6bc1\u662f\u7531Python\u7684\u5185\u5b58\u7ba1\u7406\u7cfb\u7edf\u81ea\u52a8\u5904\u7406\u7684\u3002\u4e86\u89e3\u5bf9\u8c61\u7684\u521b\u5efa\u4e0e\u9500\u6bc1\u8fc7\u7a0b\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u7406\u89e3\u53d8\u91cf\u5730\u5740\u7684\u53d8\u5316\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import gc<\/p>\n<p>class MyClass:<\/p>\n<p>    def __del__(self):<\/p>\n<p>        print(&quot;\u5bf9\u8c61\u88ab\u9500\u6bc1&quot;)<\/p>\n<p>a = MyClass()<\/p>\n<p>del a  # \u6b64\u65f6\u5bf9\u8c61\u4f1a\u88ab\u9500\u6bc1\uff0c\u5e76\u8c03\u7528__del__\u65b9\u6cd5<\/p>\n<p>gc.collect()  # \u624b\u52a8\u89e6\u53d1\u5783\u573e\u56de\u6536<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u5185\u5b58\u5730\u5740\u4e0e\u6027\u80fd\u4f18\u5316<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u7406\u89e3\u5bf9\u8c61\u7684\u5185\u5b58\u5730\u5740\u548c\u5f15\u7528\u5173\u7cfb\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u8fdb\u884c\u6027\u80fd\u4f18\u5316\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u548c\u590d\u6742\u6570\u636e\u7ed3\u6784\u65f6\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u5bf9\u8c61\u521b\u5efa<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\uff0c\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u5bf9\u8c61\u521b\u5efa\u548c\u590d\u5236\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = [1, 2, 3]<\/p>\n<p>b = a  # \u5171\u4eab\u540c\u4e00\u4e2a\u5bf9\u8c61\uff0c\u907f\u514d\u4e86\u4e0d\u5fc5\u8981\u7684\u590d\u5236<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528\u751f\u6210\u5668\u548c\u8fed\u4ee3\u5668<\/h4>\n<\/p>\n<p><p>\u751f\u6210\u5668\u548c\u8fed\u4ee3\u5668\u662f\u5904\u7406\u5927\u6570\u636e\u96c6\u7684\u6709\u6548\u5de5\u5177\uff0c\u5b83\u4eec\u53ef\u4ee5\u6309\u9700\u751f\u6210\u6570\u636e\uff0c\u907f\u514d\u4e00\u6b21\u6027\u52a0\u8f7d\u5927\u91cf\u6570\u636e\u5230\u5185\u5b58\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def my_generator():<\/p>\n<p>    for i in range(1000000):<\/p>\n<p>        yield i<\/p>\n<p>gen = my_generator()<\/p>\n<p>print(next(gen))  # \u8f93\u51fa0<\/p>\n<p>print(next(gen))  # \u8f93\u51fa1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001PYTHON\u4e2d\u7684\u5185\u5b58\u6cc4\u6f0f<\/h3>\n<\/p>\n<p><p>\u5c3d\u7ba1Python\u6709\u5783\u573e\u56de\u6536\u673a\u5236\uff0c\u4f46\u5185\u5b58\u6cc4\u6f0f\u4ecd\u7136\u53ef\u80fd\u53d1\u751f\u3002\u4e86\u89e3\u548c\u68c0\u6d4b\u5185\u5b58\u6cc4\u6f0f\uff0c\u662f\u4fdd\u969c\u4ee3\u7801\u7a33\u5b9a\u6027\u7684\u91cd\u8981\u73af\u8282\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5faa\u73af\u5f15\u7528<\/h4>\n<\/p>\n<p><p>\u5faa\u73af\u5f15\u7528\u662f\u5185\u5b58\u6cc4\u6f0f\u7684\u5e38\u89c1\u539f\u56e0\u4e4b\u4e00\u3002\u867d\u7136Python\u7684\u5783\u573e\u56de\u6536\u5668\u53ef\u4ee5\u5904\u7406\u5927\u90e8\u5206\u5faa\u73af\u5f15\u7528\uff0c\u4f46\u590d\u6742\u60c5\u51b5\u4e0b\u4ecd\u7136\u53ef\u80fd\u53d1\u751f\u5185\u5b58\u6cc4\u6f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class A:<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.b = B(self)<\/p>\n<p>class B:<\/p>\n<p>    def __init__(self, a):<\/p>\n<p>        self.a = a<\/p>\n<p>a = A()<\/p>\n<p>b = a.b<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u68c0\u6d4b\u5185\u5b58\u6cc4\u6f0f<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u5de5\u5177\u548c\u5e93\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u68c0\u6d4b\u548c\u5206\u6790\u5185\u5b58\u6cc4\u6f0f\u3002\u4f8b\u5982\uff0c<code>objgraph<\/code>\u5e93\u53ef\u4ee5\u53ef\u89c6\u5316\u5bf9\u8c61\u5f15\u7528\u5173\u7cfb\uff0c\u5e2e\u52a9\u6211\u4eec\u5b9a\u4f4d\u5185\u5b58\u6cc4\u6f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import objgraph<\/p>\n<p>objgraph.show_refs([a], filename=&#39;a_refs.png&#39;)  # \u751f\u6210\u5bf9\u8c61\u5f15\u7528\u5173\u7cfb\u56fe<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u5185\u5b58\u7ba1\u7406\u7684\u6700\u4f73\u5b9e\u8df5<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u786e\u4fddPython\u7a0b\u5e8f\u7684\u9ad8\u6548\u548c\u7a33\u5b9a\u8fd0\u884c\uff0c\u9075\u5faa\u5185\u5b58\u7ba1\u7406\u7684\u6700\u4f73\u5b9e\u8df5\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u907f\u514d\u5168\u5c40\u53d8\u91cf<\/h4>\n<\/p>\n<p><p>\u5168\u5c40\u53d8\u91cf\u5728\u7a0b\u5e8f\u7684\u6574\u4e2a\u751f\u547d\u5468\u671f\u5185\u90fd\u5b58\u5728\uff0c\u5bb9\u6613\u9020\u6210\u5185\u5b58\u6cc4\u6f0f\u3002\u5c3d\u91cf\u907f\u514d\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\uff0c\u6216\u8005\u5728\u4e0d\u9700\u8981\u65f6\u53ca\u65f6\u91ca\u653e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def my_function():<\/p>\n<p>    global a<\/p>\n<p>    a = [1, 2, 3]<\/p>\n<p>my_function()<\/p>\n<p>del a  # \u53ca\u65f6\u91ca\u653e\u5168\u5c40\u53d8\u91cf<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5408\u7406\u4f7f\u7528\u6570\u636e\u7ed3\u6784<\/h4>\n<\/p>\n<p><p>\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u5185\u5b58\u4f7f\u7528\u6548\u7387\u3002\u6bd4\u5982\uff0c\u5bf9\u4e8e\u5927\u91cf\u91cd\u590d\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528\u96c6\u5408\u6216\u5b57\u5178\u6765\u907f\u514d\u91cd\u590d\u5b58\u50a8\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = [1, 2, 2, 3, 3, 3]<\/p>\n<p>unique_data = set(data)  # \u4f7f\u7528\u96c6\u5408\u53bb\u91cd<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u6df1\u5165\u7406\u89e3Python\u7684\u5185\u5b58\u7ba1\u7406\u673a\u5236\u3001\u5bf9\u8c61\u6a21\u578b\u548c<code>id()<\/code>\u51fd\u6570\u7684\u4f7f\u7528\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u638c\u63e1\u53d8\u91cf\u5730\u5740\u7684\u6982\u5ff5\uff0c\u5e76\u5728\u5b9e\u9645\u7f16\u7a0b\u4e2d\u8fdb\u884c\u6027\u80fd\u4f18\u5316\u548c\u5185\u5b58\u7ba1\u7406\u3002\u5e0c\u671b\u672c\u6587\u7684\u8be6\u7ec6\u4ecb\u7ecd\u80fd\u5e2e\u52a9\u4f60\u5728Python\u7f16\u7a0b\u4e2d\u66f4\u52a0\u5f97\u5fc3\u5e94\u624b\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u83b7\u53d6\u53d8\u91cf\u7684\u5185\u5b58\u5730\u5740\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u51fd\u6570<code>id()<\/code>\u6765\u83b7\u53d6\u53d8\u91cf\u7684\u5185\u5b58\u5730\u5740\u3002\u8fd9\u4e2a\u51fd\u6570\u8fd4\u56de\u7684\u662f\u5bf9\u8c61\u7684\u552f\u4e00\u6807\u8bc6\u7b26\uff0c\u901a\u5e38\u60c5\u51b5\u4e0b\u5b83\u4e0e\u5bf9\u8c61\u7684\u5185\u5b58\u5730\u5740\u76f8\u540c\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">a = 10\nprint(id(a))  # \u8f93\u51fa\u53d8\u91cfa\u7684\u5185\u5b58\u5730\u5740\n<\/code><\/pre>\n<p><strong>Python\u4e2d\u53d8\u91cf\u7684\u5730\u5740\u6709\u4ec0\u4e48\u5b9e\u9645\u610f\u4e49\uff1f<\/strong><br \/>\u4e86\u89e3\u53d8\u91cf\u7684\u5185\u5b58\u5730\u5740\u6709\u52a9\u4e8e\u66f4\u6df1\u5165\u5730\u7406\u89e3Python\u7684\u5185\u5b58\u7ba1\u7406\u548c\u5bf9\u8c61\u5f15\u7528\u3002\u5f53\u591a\u4e2a\u53d8\u91cf\u5f15\u7528\u540c\u4e00\u4e2a\u5bf9\u8c61\u65f6\uff0c\u5b83\u4eec\u7684\u5730\u5740\u76f8\u540c\uff0c\u8fd9\u53ef\u4ee5\u5e2e\u52a9\u5224\u65ad\u5bf9\u8c61\u7684\u53ef\u53d8\u6027\u548c\u5185\u5b58\u6548\u7387\u3002\u5728\u8c03\u8bd5\u548c\u6027\u80fd\u4f18\u5316\u65f6\uff0c\u4e86\u89e3\u5185\u5b58\u5730\u5740\u4e5f\u80fd\u591f\u63d0\u4f9b\u6709\u4ef7\u503c\u7684\u4fe1\u606f\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u5224\u65ad\u4e24\u4e2a\u53d8\u91cf\u662f\u5426\u6307\u5411\u540c\u4e00\u4e2a\u5bf9\u8c61\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>is<\/code>\u8fd0\u7b97\u7b26\u6765\u5224\u65ad\u4e24\u4e2a\u53d8\u91cf\u662f\u5426\u6307\u5411\u540c\u4e00\u4e2a\u5bf9\u8c61\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">a = [1, 2, 3]\nb = a\nprint(a is b)  # \u8f93\u51faTrue\uff0c\u8868\u793aa\u548cb\u6307\u5411\u540c\u4e00\u4e2a\u5bf9\u8c61\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u5224\u65ad\u5bf9\u8c61\u7684\u5171\u4eab\u4e0e\u590d\u5236\u60c5\u51b5\uff0c\u4ece\u800c\u8fdb\u884c\u66f4\u7cbe\u786e\u7684\u5185\u5b58\u7ba1\u7406\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u53ef\u4ee5\u901a\u8fc7\u5185\u7f6e\u51fd\u6570id()\u6765\u83b7\u53d6\u53d8\u91cf\u7684\u5730\u5740\u3001id()\u51fd\u6570\u8fd4\u56de\u5bf9\u8c61\u7684\u552f\u4e00\u6807\u8bc6\u7b26\u3001\u901a\u5e38\u662f\u5bf9\u8c61\u5728\u5185\u5b58\u4e2d\u7684 [&hellip;]","protected":false},"author":3,"featured_media":1160874,"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\/1160867"}],"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=1160867"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1160867\/revisions"}],"predecessor-version":[{"id":1160875,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1160867\/revisions\/1160875"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1160874"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1160867"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1160867"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1160867"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}