{"id":300585,"date":"2024-05-20T17:46:43","date_gmt":"2024-05-20T09:46:43","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/300585.html"},"modified":"2024-05-20T17:46:57","modified_gmt":"2024-05-20T09:46:57","slug":"python-%e5%a6%82%e4%bd%95%e5%9c%a8%e7%b1%bb%e5%9e%8b%e6%a0%87%e6%b3%a8%e6%97%b6%e4%bd%bf%e7%94%a8%e8%87%aa%e8%ba%ab%e7%b1%bb","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/300585.html","title":{"rendered":"Python \u5982\u4f55\u5728\u7c7b\u578b\u6807\u6ce8\u65f6\u4f7f\u7528\u81ea\u8eab\u7c7b"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26212806\/8e64cfb6-30d1-4da5-9136-1466899b1bcc.webp\" alt=\"Python \u5982\u4f55\u5728\u7c7b\u578b\u6807\u6ce8\u65f6\u4f7f\u7528\u81ea\u8eab\u7c7b\" \/><\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u76f4\u63a5\u5728\u7c7b\u578b\u6807\u6ce8\u65f6\u4f7f\u7528\u81ea\u8eab\u7c7b\u53ef\u4ee5\u901a\u8fc7\u4ece<code>__future__<\/code>\u5bfc\u5165<code>annotations<\/code>\u7279\u6027\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u8868\u793a\u6cd5\u6216\u8005\u5229\u7528<code>from __future__ import annotations<\/code>\u5b9e\u73b0\u3002<strong>\u8fd9\u5176\u4e2d\uff0c\u4ece<code>__future__<\/code>\u5bfc\u5165<code>annotations<\/code>\u7279\u6027\u662f\u6700\u63a8\u8350\u7684\u65b9\u5f0f<\/strong>\uff0c\u56e0\u4e3a\u5b83\u80fd\u8ba9Python\u5ef6\u8fdf\u5bf9\u6ce8\u89e3\u7684\u8bc4\u4f30\uff0c\u4ece\u800c\u5141\u8bb8\u5728\u5b9a\u4e49\u7c7b\u7684\u540c\u65f6\u5728\u7c7b\u578b\u6ce8\u89e3\u4e2d\u5f15\u7528\u5b83\u81ea\u5df1\u3002\u8fd9\u5bf9\u4e8e\u5b9a\u4e49\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\u548c\u5b9e\u73b0\u67d0\u4e9b\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u5982\u5de5\u5382\u6a21\u5f0f\u548c\u5355\u4f8b\u6a21\u5f0f\uff0c\u5728\u7f16\u5199\u9762\u5411\u5bf9\u8c61\u7684\u7a0b\u5e8f\u65f6\u5c24\u5176\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h3><strong>\u4e00\u3001\u4ece<code>__future__<\/code>\u5bfc\u5165<code>annotations<\/code>\u7279\u6027<\/strong><\/h3>\n<\/p>\n<p><p>\u4ece Python 3.7 \u5f00\u59cb\uff0c\u901a\u8fc7<code>from __future__ import annotations<\/code>\u4f7f\u6240\u6709\u5728\u6a21\u5757\u6216\u5305\u5c42\u7ea7\u7684\u6ce8\u89e3\u53d8\u4e3a\u5ef6\u8fdf\u8ba1\u7b97\u3002\u8fd9\u610f\u5473\u7740\u7c7b\u578b\u6ce8\u89e3\u4e2d\u7684\u4ee3\u7801\u4f1a\u88ab\u5b58\u50a8\u4e3a\u5b57\u7b26\u4e32\uff0c\u800c\u4e0d\u662f\u5728\u5b9a\u4e49\u65f6\u7acb\u5373\u6267\u884c\u3002\u8fd9\u4e2a\u7279\u6027\u9ed8\u8ba4\u5728Python 3.10\u53ca\u4ee5\u540e\u7684\u7248\u672c\u4e2d\u542f\u7528\u3002<\/p>\n<\/p>\n<p><p>\u8981\u4f7f\u7528\u8fd9\u4e2a\u7279\u6027\uff0c\u9996\u5148\u5728\u6587\u4ef6\u5f00\u5934\u5bfc\u5165<code>annotations<\/code>\u7279\u6027\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from __future__ import annotations<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e4b\u540e\uff0c\u5373\u53ef\u5728\u7c7b\u5185\u90e8\u76f4\u63a5\u4f7f\u7528\u81ea\u8eab\u7c7b\u578b\u4f5c\u4e3a\u6ce8\u89e3\uff0c\u65e0\u9700\u989d\u5916\u7684\u5f15\u53f7\u6216\u5176\u4ed6\u6280\u5de7\u3002\u8fd9\u5bf9\u4e8e\u5b9a\u4e49\u9012\u5f52\u6570\u636e\u7ed3\u6784\u975e\u5e38\u6709\u7528\u3002\u4f8b\u5982\uff0c\u5b9a\u4e49\u4e00\u4e2a\u6811\u72b6\u7ed3\u6784\uff0c\u6bcf\u4e2a\u8282\u70b9\u53ef\u80fd\u5305\u542b\u6307\u5411\u81ea\u8eab\u7c7b\u578b\u7684\u5f15\u7528\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class TreeNode:<\/p>\n<p>    def __init__(self, value: int, left: TreeNode = None, right: TreeNode = None):<\/p>\n<p>        self.value = value<\/p>\n<p>        self.left = left<\/p>\n<p>        self.right = right<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3><strong>\u4e8c\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u8868\u793a\u6cd5<\/strong><\/h3>\n<\/p>\n<p><p>\u5728<code>annotations<\/code>\u7279\u6027\u666e\u53ca\u4e4b\u524d\uff0cPython\u5141\u8bb8\u4f7f\u7528\u5b57\u7b26\u4e32\u6765\u907f\u514d\u5728\u7c7b\u5b9a\u4e49\u4e4b\u524d\u5c31\u5f15\u7528\u5b83\u81ea\u8eab\u3002\u8fd9\u79cd\u65b9\u5f0f\u7b80\u5355\u4f46\u7a0d\u663e\u7b28\u62d9\uff0c\u9700\u8981\u5c06\u7c7b\u578b\u6ce8\u89e3\u653e\u5728\u5f15\u53f7\u91cc\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class ListNode:<\/p>\n<p>    def __init__(self, value: int, next: &#039;ListNode&#039; = None):<\/p>\n<p>        self.value = value<\/p>\n<p>        self.next = next<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u7f3a\u70b9\u5728\u4e8e\u5b83\u4e0d\u90a3\u4e48\u76f4\u89c2\uff0c\u7279\u522b\u662f\u5728\u8fdb\u884c\u9759\u6001\u7c7b\u578b\u68c0\u67e5\u6216\u8005\u67e5\u770b\u4ee3\u7801\u7684\u81ea\u52a8\u5316\u6587\u6863\u751f\u6210\u65f6\uff0c\u53ef\u80fd\u4e0d\u5982\u5b9e\u9645\u7c7b\u578b\u6e05\u6670\u3002<\/p>\n<\/p>\n<p><h3><strong>\u4e09\u3001\u5229\u7528<code>from __future__ import annotations<\/code>\u5b9e\u73b0<\/strong><\/h3>\n<\/p>\n<p><p>\u867d\u7136\u8fd9\u79cd\u65b9\u6cd5\u5df2\u7ecf\u5728\u524d\u9762\u63d0\u5230\u8fc7\uff0c\u4f46\u503c\u5f97\u8fdb\u4e00\u6b65\u63a2\u8ba8\u5176\u5bf9\u4ee3\u7801\u7684\u5177\u4f53\u5f71\u54cd\u3002\u5728\u542f\u7528\u8fd9\u4e2a\u7279\u6027\u540e\uff0c\u6240\u6709\u7684\u6ce8\u89e3\u90fd\u4f1a\u88ab\u5b58\u50a8\u4e3a\u5b83\u4eec\u7684\u5b57\u9762\u91cf\u5b57\u7b26\u4e32\u3002\u8fd9\u4e0d\u4ec5\u4ec5\u4f7f\u5f97\u5728\u7c7b\u578b\u6ce8\u89e3\u4e2d\u4f7f\u7528\u81ea\u8eab\u7c7b\u53d8\u5f97\u53ef\u80fd\uff0c\u8fd8\u7b80\u5316\u4e86\u5bf9\u5faa\u73af\u5f15\u7528\u7684\u5904\u7406\u3002<\/p>\n<\/p>\n<p><p>\u5177\u4f53\u5230\u5b9e\u8df5\u4e2d\uff0c\u8fd9\u610f\u5473\u7740\u5728\u591a\u6587\u4ef6\u9879\u76ee\u4e2d\uff0c\u5373\u4f7f\u4e24\u4e2a\u6a21\u5757\u5f7c\u6b64\u5f15\u7528\uff0c\u4e5f\u4e0d\u5fc5\u62c5\u5fc3\u5bfc\u5165\u65f6\u4ea7\u751f\u7684\u5faa\u73af\u4f9d\u8d56\u95ee\u9898\uff0c\u56e0\u4e3a\u6ce8\u89e3\u4e0d\u4f1a\u5728\u5bfc\u5165\u65f6\u7acb\u5373\u6c42\u503c\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u4ee3\u7801\u7ed3\u6784\u66f4\u52a0\u7075\u6d3b\uff0c\u9879\u76ee\u7684\u6a21\u5757\u5316\u548c\u91cd\u6784\u4e5f\u66f4\u52a0\u65b9\u4fbf\u3002<\/p>\n<\/p>\n<p><h3><strong>\u56db\u3001\u603b\u7ed3<\/strong><\/h3>\n<\/p>\n<p><p>\u5728\u7c7b\u578b\u6807\u6ce8\u4e2d\u4f7f\u7528\u81ea\u8eab\u7c7b\u662f\u7f16\u5199\u590d\u6742Python\u7a0b\u5e8f\u65f6\u5e38\u89c1\u7684\u9700\u6c42\u3002<strong>\u4ece<code>__future__<\/code>\u5bfc\u5165<code>annotations<\/code>\u7279\u6027\u662f\u6700\u4f18\u96c5\u4e14\u5b98\u65b9\u63a8\u8350\u7684\u65b9\u6cd5<\/strong>\u3002\u8fd9\u4e0d\u4ec5\u80fd\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u7ef4\u62a4\u6027\uff0c\u8fd8\u80fd\u5728\u4e0d\u727a\u7272\u6027\u80fd\u7684\u60c5\u51b5\u4e0b\u514b\u670d\u5faa\u73af\u5f15\u7528\u7684\u95ee\u9898\u3002\u968f\u7740Python\u8bed\u8a00\u7684\u4e0d\u65ad\u53d1\u5c55\uff0c\u7406\u89e3\u5e76\u8fd0\u7528\u8fd9\u4e9b\u9ad8\u7ea7\u7279\u6027\u5c06\u662f\u63d0\u9ad8\u4ee3\u7801\u8d28\u91cf\u7684\u5173\u952e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u5982\u4f55\u5728 Python \u4e2d\u4f7f\u7528\u81ea\u8eab\u7c7b\u8fdb\u884c\u7c7b\u578b\u6807\u6ce8\uff1f<\/strong><\/p>\n<p>\u7c7b\u578b\u6807\u6ce8\u662f Python 3.5 \u5f15\u5165\u7684\u4e00\u79cd\u529f\u80fd\uff0c\u5b83\u5141\u8bb8\u6211\u4eec\u5728\u4ee3\u7801\u4e2d\u6307\u5b9a\u53d8\u91cf\u3001\u51fd\u6570\u53c2\u6570\u4ee5\u53ca\u51fd\u6570\u8fd4\u56de\u503c\u7684\u7c7b\u578b\u3002\u5f53\u6211\u4eec\u9700\u8981\u5728\u7c7b\u578b\u6807\u6ce8\u4e2d\u4f7f\u7528\u81ea\u8eab\u7c7b\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u7279\u6b8a\u7684\u5b57\u7b26\u4e32 &quot;self&quot; \u8868\u793a\u5f53\u524d\u7c7b\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre><code class=\"language-python\">class MyClass:\n    def __init__(self, value: &#039;MyClass&#039;) -&gt; None:\n        self.value = value\n    \n    def multiply(self, other: &#039;MyClass&#039;) -&gt; &#039;MyClass&#039;:\n        return MyClass(self.value * other.value)\n<\/code><\/pre>\n<p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u7c7b\u578b <code>MyClass<\/code> \u6765\u6807\u6ce8\u4e86 <code>__init__<\/code> \u65b9\u6cd5\u7684\u53c2\u6570\u548c\u8fd4\u56de\u503c\u3002\u540c\u6837\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u5728 <code>multiply<\/code> \u65b9\u6cd5\u4e2d\u4f7f\u7528 <code>MyClass<\/code> \u6807\u6ce8\u53c2\u6570\u548c\u8fd4\u56de\u503c\u3002<\/p>\n<p><strong>2. \u662f\u5426\u5fc5\u987b\u5728\u7c7b\u578b\u6807\u6ce8\u4e2d\u4f7f\u7528\u5b57\u7b26\u4e32\u8868\u793a\u81ea\u8eab\u7c7b\uff1f<\/strong><\/p>\n<p>\u867d\u7136\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\u4f7f\u7528\u5b57\u7b26\u4e32 &quot;self&quot; \u662f\u4e00\u79cd\u5e38\u89c1\u7684\u65b9\u5f0f\uff0c\u4f46\u5b83\u4e0d\u662f\u5f3a\u5236\u7684\u3002\u5b9e\u9645\u4e0a\uff0c\u5982\u679c\u4f60\u7684\u7c7b\u540d\u79f0\u4e0e\u53c2\u6570\u6216\u8fd4\u56de\u503c\u6ca1\u6709\u51b2\u7a81\uff0c\u4f60\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u7c7b\u540d\u6765\u8fdb\u884c\u7c7b\u578b\u6807\u6ce8\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre><code class=\"language-python\">class MyClass:\n    def __init__(self, value: MyClass) -&gt; None:\n        self.value = value\n    \n    def multiply(self, other: MyClass) -&gt; MyClass:\n        return MyClass(self.value * other.value)\n<\/code><\/pre>\n<p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u76f4\u63a5\u4f7f\u7528\u4e86\u7c7b\u540d <code>MyClass<\/code> \u6765\u6807\u6ce8\u4e86 <code>__init__<\/code> \u65b9\u6cd5\u7684\u53c2\u6570\u548c\u8fd4\u56de\u503c\uff0c\u540c\u6837\u4e5f\u5728 <code>multiply<\/code> \u65b9\u6cd5\u4e2d\u8fdb\u884c\u4e86\u7c7b\u4f3c\u7684\u6807\u6ce8\u3002<\/p>\n<p><strong>3. \u4e3a\u4ec0\u4e48\u8981\u5728\u7c7b\u578b\u6807\u6ce8\u4e2d\u4f7f\u7528\u81ea\u8eab\u7c7b\uff1f<\/strong><\/p>\n<p>\u4f7f\u7528\u81ea\u8eab\u7c7b\u8fdb\u884c\u7c7b\u578b\u6807\u6ce8\u6709\u52a9\u4e8e\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u5b83\u53ef\u4ee5\u4f7f\u5176\u4ed6\u4eba\u66f4\u5bb9\u6613\u7406\u89e3\u4ee3\u7801\u4e2d\u7684\u7c7b\u578b\u5173\u7cfb\uff0c\u5e76\u80fd\u591f\u66f4\u597d\u5730\u8fdb\u884c\u4ee3\u7801\u5206\u6790\u548c\u9519\u8bef\u68c0\u67e5\u3002\u6b64\u5916\uff0c\u7c7b\u578b\u6807\u6ce8\u8fd8\u53ef\u4ee5\u4e3a\u5f00\u53d1\u5de5\u5177\u63d0\u4f9b\u66f4\u597d\u7684\u4ee3\u7801\u63d0\u793a\u548c\u81ea\u52a8\u8865\u5168\u529f\u80fd\uff0c\u4ece\u800c\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u3002\u56e0\u6b64\uff0c\u5728\u5408\u9002\u7684\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528\u81ea\u8eab\u7c7b\u8fdb\u884c\u7c7b\u578b\u6807\u6ce8\u662f\u4e00\u79cd\u5f88\u597d\u7684\u5b9e\u8df5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u76f4\u63a5\u5728\u7c7b\u578b\u6807\u6ce8\u65f6\u4f7f\u7528\u81ea\u8eab\u7c7b\u53ef\u4ee5\u901a\u8fc7\u4ece__future__\u5bfc\u5165annotations\u7279\u6027\u3001\u4f7f\u7528 [&hellip;]","protected":false},"author":3,"featured_media":300600,"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\/300585"}],"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=300585"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/300585\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/300600"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=300585"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=300585"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=300585"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}