﻿{"id":21854,"date":"2023-06-16T19:07:49","date_gmt":"2023-06-16T11:07:49","guid":{"rendered":"https:\/\/www.linuxmi.com\/?p=21854"},"modified":"2023-06-16T19:07:49","modified_gmt":"2023-06-16T11:07:49","slug":"python-3-12-debugging","status":"publish","type":"post","link":"https:\/\/www.linuxmi.com\/python-3-12-debugging.html","title":{"rendered":"\u63a2\u7d22 Python 3.12\uff1a\u8ba9\u4f60\u7684\u8c03\u8bd5\u6280\u80fd\u7ffb\u500d\u63d0\u5347\uff01"},"content":{"rendered":"<p>Python\u8ba1\u5212\u4e8e2023\u5e7410\u6708\u53d1\u5e033.12\u7248\u672c\u3002\u8fd9\u4e2a\u7248\u672c\u5e26\u6765\u4e86\u4e00\u7cfb\u5217\u4ee4\u4eba\u6fc0\u52a8\u7684\u529f\u80fd\u548c\u6539\u8fdb\u3002\u5b83\u7684\u76ee\u6807\u662f\u901a\u8fc7\u63d0\u4f9b\u4e00\u4e2a\u66f4\u9ad8\u6548\u3001\u529f\u80fd\u66f4\u5f3a\u5927\u7684\u5e73\u53f0\u6765\u6539\u5584\u4f60\u7684\u7f16\u7801\u4f53\u9a8c\uff0c\u4ece\u800c\u63d0\u5347\u4f60\u7684\u7f16\u7a0b\u6280\u80fd\u3002<\/p>\n<h2><strong>1\u3001\u589e\u5f3a\u7684\u9519\u8bef\u4fe1\u606f <\/strong><\/h2>\n<p>Python\u5728\u6700\u8fd1\u7684\u7248\u672c\u4e2d\u4e0d\u65ad\u6539\u8fdb\u5176\u9519\u8bef\u4fe1\u606f\u30023.11\u7248\u672c\u901a\u8fc7\u6307\u793a\u5728\u884c\u4e2d\u7684\u786e\u5207\u4f4d\u7f6e\uff0c\u63d0\u4f9b\u4e86\u66f4\u51c6\u786e\u7684\u9519\u8bef\u4fe1\u606f\u3002<\/p>\n<p>Python 3.12\u5c06\u8fd9\u4e9b\u589e\u5f3a\u529f\u80fd\u63d0\u5347\u5230\u4e86\u4e00\u4e2a\u65b0\u7684\u6c34\u5e73\uff0c\u5177\u4f53\u5305\u62ec\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n<ul>\n<li>\u5bf9\u4e8e\u7f3a\u5c11\u6a21\u5757\u7684\u5efa\u8bae\uff08&#8221;\u4f60\u662f\u5426\u5fd8\u8bb0\u5bfc\u5165X\uff1f&#8221;\uff09\uff0c\u73b0\u5728\u5305\u62ec\u4e86\u6765\u81ea\u6807\u51c6\u5e93\u7684\u6a21\u5757\u3002<\/li>\n<li>\u5bf9\u4e8e\u4e0e\u5bfc\u5165\u76f8\u5173\u7684\u5e38\u89c1\u8bed\u6cd5\u9519\u8bef\uff0c\u6539\u8fdb\u4e86\u9519\u8bef\u5efa\u8bae\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u7684\u4ee3\u7801\u4e2d\u6709import p from m\uff0c\u5c06\u4f1a\u63d0\u793a\u4e00\u4e2a\u9519\u8bef\uff0c\u8be2\u95ee\u662f\u5426\u6253\u7b97\u5199\u6210from m import p\u3002<\/li>\n<li>\u5bf9\u4e8e\u7ed9\u5b9a\u6a21\u5757\u7684\u5bfc\u5165\u9519\u8bef\uff0c\u73b0\u5728\u8fd8\u5305\u62ec\u6765\u81ea\u5bfc\u5165\u6a21\u5757\u7684\u547d\u540d\u7a7a\u95f4\u7684\u5efa\u8bae\u3002<\/li>\n<li>NameError\u7684\u5efa\u8bae\u73b0\u5728\u5728\u7c7b\u5b9e\u4f8b\u4e2d\u629b\u51fa\u65f6\u4e5f\u4f1a\u5305\u542bself\u3002\u4f8b\u5982\uff0c\u5982\u679c\u540d\u79f0speed\u672a\u5b9a\u4e49\uff0c\u9519\u8bef\u6d88\u606f\u5c06\u63d0\u51faself.speed\u4f5c\u4e3a\u6f5c\u5728\u7684\u4fee\u6b63\u5efa\u8bae\u3002\u8fd9\u7279\u522b\u6709\u7528\uff0c\u56e0\u4e3a\u5728\u7c7b\u5b9e\u4f8b\u4e2d\u5ffd\u7565self\u800c\u5bfc\u81f4\u5b9e\u4f8b\u53d8\u91cf\u9519\u8bef\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u9519\u8bef\u6e90\u3002<\/li>\n<\/ul>\n<h2><strong>2\u3001\u5bf9Linux Perf Profiler\u7684\u652f\u6301<\/strong><\/h2>\n<p>\u5e7f\u6cdb\u4f7f\u7528\u7684Linux\u6027\u80fd\u5206\u6790\u5de5\u5177perf\u73b0\u5728\u53ef\u4ee5\u4e0ePython 3.12\u7a0b\u5e8f\u4e00\u8d77\u4f7f\u7528\u3002\u5728\u8fc7\u53bb\uff0c\u5b83\u53ea\u4e13\u6ce8\u4e8e\u8ddf\u8e2aPython\u8fd0\u884c\u65f6\u5185\u7684C\u7ea7\u64cd\u4f5c\u3002<\/p>\n<p>\u901a\u8fc7Python 3.12\uff0c\u4f60\u73b0\u5728\u53ef\u4ee5\u6fc0\u6d3bperf\u6765\u6536\u96c6\u6709\u5173Python\u7a0b\u5e8f\u7684\u8be6\u7ec6\u4fe1\u606f\u3002\u4f60\u53ef\u4ee5\u5728\u73af\u5883\u7ea7\u522b\u6216\u8005\u5728Python\u7a0b\u5e8f\u4e2d\u4f7f\u7528sys.activate_stack_trampoline\u51fd\u6570\u6765\u6fc0\u6d3b\u5b83\u3002<\/p>\n<h2><strong>3\u3001\u7c7b\u578b\u63d0\u793a\u7684\u6539\u8fdb<\/strong><\/h2>\n<p>\u81ea\u4ecePython 3.5\u5f15\u5165\u7c7b\u578b\u63d0\u793a\u8bed\u6cd5\u4ee5\u6765\uff0cPython\u7684\u7c7b\u578b\u63d0\u793a\u8bed\u6cd5\u4f7f\u5f97\u4ee3\u7801\u68c0\u67e5\u5de5\u5177\u80fd\u591f\u63d0\u524d\u68c0\u6d4b\u51fa\u5404\u79cd\u9519\u8bef\u3002\u968f\u7740\u6bcf\u4e2a\u7248\u672c\u7684\u53d1\u5e03\uff0cPython\u7684\u7c7b\u578b\u7cfb\u7edf\u4e0d\u65ad\u53d1\u5c55\uff0c\u6db5\u76d6\u4e86\u66f4\u5e7f\u6cdb\u7684\u7528\u4f8b\uff0c\u5e76\u63d0\u4f9b\u4e86\u589e\u5f3a\u7684\u7c7b\u578b\u63d0\u793a\u529f\u80fd\u3002<\/p>\n<p>TypedDict Python 3.12\u5f15\u5165\u4e86TypedDict\u4f5c\u4e3a\u51fd\u6570\u5173\u952e\u5b57\u53c2\u6570\u7c7b\u578b\u63d0\u793a\u7684\u6765\u6e90\u3002\u8fd9\u5229\u7528\u4e86\u7248\u672c3.11\u5f15\u5165\u7684Unpack\u53ef\u53d8\u6cdb\u578b\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u6f14\u793a\u4e86\u8fd9\u4e2a\u529f\u80fd\uff1a<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"null\">class Movie(TypedDict):\r\n    name: str\r\n    year: int\r\n\r\ndef foo(**kwargs: Unpack[Movie]) -&gt; None: ...<\/code><\/pre>\n<p>\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u51fd\u6570foo\u53ef\u4ee5\u63a5\u6536\u4e0eMovie TypedDict\u7684\u5185\u5bb9\u76f8\u7b26\u7684\u540d\u79f0\u548c\u7c7b\u578b\u7684\u5173\u952e\u5b57\u53c2\u6570\uff1a\u7c7b\u578b\u4e3astr\u7684name\u548c\u7c7b\u578b\u4e3aint\u7684year\u3002<\/p>\n<p>\u8fd9\u4e2a\u589e\u5f3a\u5728\u4e3a\u63a5\u53d7\u6ca1\u6709\u9ed8\u8ba4\u503c\u7684\u53ef\u9009\u5173\u952e\u5b57\u53c2\u6570\u7684\u51fd\u6570\u8fdb\u884c\u7c7b\u578b\u63d0\u793a\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<p>\u7c7b\u578b\u53c2\u6570\u8bed\u6cd5 Python 3.12\u5f15\u5165\u4e86\u4e00\u79cd\u66f4\u7b80\u6d01\u7684\u8bed\u6cd5\uff0c\u7528\u4e8e\u6307\u5b9a\u6cdb\u578b\u7c7b\u3001\u51fd\u6570\u6216\u7c7b\u578b\u522b\u540d\u4e2d\u7684\u7c7b\u578b\u3002<\/p>\n<p>\u8003\u8651\u4ee5\u4e0b\u793a\u4f8b\uff1a<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"null\"># \u65e7\u65b9\u6cd5\r\nfrom typing import TypeVar\r\n_T = TypeVar(\"_T\")\r\ndef func(a: _T, b: _T) -&gt; _T:\r\n    ...\r\n# \u65b0\u578b\u53c2\u6570\u6cd5\r\ndef func[T](a: T, b: T) -&gt; T:\r\n    ...<\/code><\/pre>\n<p>\u4f7f\u7528\u65b0\u7684\u8bed\u6cd5\uff0c\u65e0\u9700\u663e\u5f0f\u5bfc\u5165TypeVar\u3002\u76f8\u53cd\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528func[T]\u8bed\u6cd5\u8868\u793a\u6cdb\u578b\u7c7b\u578b\u5f15\u7528\u3002\u8fd8\u53ef\u4ee5\u5b9a\u4e49\u7c7b\u578b\u7684\u8fb9\u754c\uff0c\u4f8b\u5982\u6307\u5b9a\u7c7b\u578b\u5fc5\u987b\u662f\u4e00\u7ec4\u7c7b\u578b\u4e2d\u7684\u5176\u4e2d\u4e00\u4e2a\u3002\u4f46\u662f\uff0c\u8fd9\u4e9b\u7c7b\u578b\u672c\u8eab\u4e0d\u80fd\u662f\u6cdb\u578b\u7684\u3002\u6b64\u8bed\u6cd5\u7684\u4e00\u4e2a\u793a\u4f8b\u662ffunc[T: (str, int)]\u3002<\/p>\n<h2><strong>4\u3001\u5783\u573e\u56de\u6536<\/strong><\/h2>\n<p>\u5728Python 3.12\u4e2d\uff0c\u5783\u573e\u56de\u6536\uff08GC\uff09\u673a\u5236\u53d1\u751f\u4e86\u6539\u53d8\u3002\u4ee5\u524d\uff0c\u5783\u573e\u56de\u6536\u4f1a\u5728\u5206\u914d\u5bf9\u8c61\u65f6\u8fd0\u884c\u3002<\/p>\n<p>\u7136\u800c\uff0c\u4ecePython 3.12\u5f00\u59cb\uff0c\u5783\u573e\u56de\u6536\u4ec5\u5728Python\u5b57\u8282\u7801\u5faa\u73af\u4e2d\u7684eval\u65ad\u70b9\u673a\u5236\u5185\u8fd0\u884c\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u5b83\u5728\u6267\u884c\u4e00\u4e2a\u5b57\u8282\u7801\u548c\u4e0b\u4e00\u4e2a\u5b57\u8282\u7801\u4e4b\u95f4\u8fd0\u884c\u3002<\/p>\n<p>\u6b64\u5916\uff0c\u5f53CPython\u7684\u4fe1\u53f7\u5904\u7406\u7a0b\u5e8f\u68c0\u67e5\u673a\u5236\u88ab\u8c03\u7528\u65f6\uff0c\u5783\u573e\u56de\u6536\u4e5f\u4f1a\u89e6\u53d1\u3002\u8fd9\u4e2a\u8c03\u6574\u4f7f\u5f97\u5728\u8fd0\u884c\u65f6\u5916\u957f\u65f6\u95f4\u8fd0\u884c\u7684C\u6269\u5c55\u8c03\u7528\u671f\u95f4\u53ef\u4ee5\u8fdb\u884c\u5b9a\u671f\u7684\u5783\u573e\u56de\u6536\u3002<\/p>\n<h2><strong>\u63ed\u5f00Python 3.12\u7684\u9762\u7eb1 <\/strong><\/h2>\n<p>Python 3.12\u5f15\u5165\u4e86\u4e00\u7cfb\u5217\u4ee4\u4eba\u6fc0\u52a8\u7684\u529f\u80fd\u548c\u6539\u8fdb\uff0c\u4e3a\u60a8\u7684\u7f16\u7801\u4f53\u9a8c\u5e26\u6765\u9769\u547d\u6027\u7684\u53d8\u5316\u3002\u901a\u8fc7\u6539\u8fdb\u7684\u9519\u8bef\u4fe1\u606f\u3001\u589e\u5f3a\u7684\u7c7b\u578b\u80fd\u529b\u3001\u5bf9Linux Perf Profiler\u7684\u652f\u6301\u4ee5\u53ca\u4f18\u5316\u7684\u5783\u573e\u56de\u6536\uff0cPython 3.12\u4e3a\u60a8\u63d0\u4f9b\u4e86\u7f16\u5199\u9ad8\u8d28\u91cf\u3001\u9ad8\u6548\u7684\u4ee3\u7801\u6240\u9700\u7684\u5de5\u5177\u3002\u62e5\u62b1Python\u7684\u529b\u91cf\uff0c\u5c06\u60a8\u7684\u7f16\u7801\u6280\u80fd\u63d0\u5347\u5230\u65b0\u7684\u9ad8\u5ea6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python\u8ba1\u5212\u4e8e2023\u5e7410\u6708\u53d1\u5e033.12\u7248\u672c\u3002\u8fd9\u4e2a\u7248\u672c\u5e26\u6765\u4e86\u4e00\u7cfb\u5217\u4ee4\u4eba\u6fc0\u52a8\u7684\u529f\u80fd\u548c\u6539\u8fdb\u3002\u5b83\u7684\u76ee &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4,2],"tags":[44],"class_list":["post-21854","post","type-post","status-publish","format-standard","hentry","category-python","category-zixun","tag-python"],"aioseo_notices":[],"views":473,"_links":{"self":[{"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/posts\/21854","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/comments?post=21854"}],"version-history":[{"count":3,"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/posts\/21854\/revisions"}],"predecessor-version":[{"id":25116,"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/posts\/21854\/revisions\/25116"}],"wp:attachment":[{"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/media?parent=21854"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/categories?post=21854"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/tags?post=21854"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}