{"id":1037072,"date":"2024-12-31T12:12:05","date_gmt":"2024-12-31T04:12:05","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1037072.html"},"modified":"2024-12-31T12:12:08","modified_gmt":"2024-12-31T04:12:08","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e8%a1%a8%e7%a4%ba%e6%97%a0%e8%bf%94%e5%9b%9e%e5%80%bc","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1037072.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u8868\u793a\u65e0\u8fd4\u56de\u503c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/9b9f0041-6c0d-4628-90e0-f094ab712ae6.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u4e2d\u5982\u4f55\u8868\u793a\u65e0\u8fd4\u56de\u503c\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u8868\u793a\u65e0\u8fd4\u56de\u503c\u7684\u65b9\u6cd5\u6709\u591a\u79cd<\/strong>\uff0c<strong>\u4f7f\u7528<code>None<\/code>\u3001\u901a\u8fc7\u7701\u7565<code>return<\/code>\u8bed\u53e5<\/strong>\u3002\u5728Python\u4e2d\uff0c\u51fd\u6570\u9ed8\u8ba4\u8fd4\u56de<code>None<\/code>\uff0c\u5373\u4f7f\u6ca1\u6709\u663e\u5f0f\u5730\u4f7f\u7528<code>return<\/code>\u8bed\u53e5\u3002\u5982\u679c\u60f3\u660e\u786e\u8868\u793a\u4e00\u4e2a\u51fd\u6570\u6ca1\u6709\u8fd4\u56de\u503c\uff0c\u53ef\u4ee5\u5728\u51fd\u6570\u4e2d\u4f7f\u7528<code>return None<\/code>\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u8fd9\u4e9b\u65b9\u6cd5\u548c\u5b83\u4eec\u7684\u4f7f\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528<code>None<\/code>\u4f5c\u4e3a\u8fd4\u56de\u503c<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c<code>None<\/code>\u662f\u4e00\u4e2a\u7279\u6b8a\u7684\u5e38\u91cf\uff0c\u7528\u6765\u8868\u793a\u201c\u4ec0\u4e48\u90fd\u6ca1\u6709\u201d\u3002\u5f53\u4f60\u5e0c\u671b\u4e00\u4e2a\u51fd\u6570\u6ca1\u6709\u8fd4\u56de\u503c\u65f6\uff0c\u53ef\u4ee5\u663e\u5f0f\u5730\u4f7f\u7528<code>return None<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def no_return():<\/p>\n<p>    return None<\/p>\n<p>result = no_return()<\/p>\n<p>print(result)  # \u8f93\u51fa: None<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u4f60\u53ef\u4ee5\u660e\u786e\u5730\u544a\u8bc9\u8c03\u7528\u8005\uff0c\u8fd9\u4e2a\u51fd\u6570\u6ca1\u6709\u4efb\u4f55\u6709\u610f\u4e49\u7684\u8fd4\u56de\u503c\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u901a\u8fc7\u7701\u7565<code>return<\/code>\u8bed\u53e5<\/p>\n<\/p>\n<p><p>Python\u51fd\u6570\u5728\u6ca1\u6709\u6307\u5b9a<code>return<\/code>\u8bed\u53e5\u65f6\uff0c\u4f1a\u9ed8\u8ba4\u8fd4\u56de<code>None<\/code>\u3002\u56e0\u6b64\uff0c\u4f60\u4e5f\u53ef\u4ee5\u901a\u8fc7\u7701\u7565<code>return<\/code>\u8bed\u53e5\u6765\u8868\u793a\u4e00\u4e2a\u51fd\u6570\u6ca1\u6709\u8fd4\u56de\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def no_return():<\/p>\n<p>    pass<\/p>\n<p>result = no_return()<\/p>\n<p>print(result)  # \u8f93\u51fa: None<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u5f0f\u66f4\u52a0\u7b80\u6d01\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u663e\u5f0f\u5730\u4f7f\u7528<code>return None<\/code>\u53ef\u80fd\u4f1a\u4f7f\u4ee3\u7801\u66f4\u5177\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u5728\u51fd\u6570\u7b7e\u540d\u4e2d\u4f7f\u7528\u6ce8\u91ca\u6216\u7c7b\u578b\u63d0\u793a<\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\uff0c\u4f60\u53ef\u4ee5\u5728\u51fd\u6570\u7b7e\u540d\u4e2d\u4f7f\u7528\u6ce8\u91ca\u6216\u7c7b\u578b\u63d0\u793a\uff0c\u660e\u786e\u8868\u793a\u51fd\u6570\u6ca1\u6709\u8fd4\u56de\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def no_return() -&gt; None:<\/p>\n<p>    pass<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u5f0f\u5728\u5927\u578b\u9879\u76ee\u4e2d\u5c24\u4e3a\u6709\u7528\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u5e2e\u52a9\u5176\u4ed6\u5f00\u53d1\u4eba\u5458\u5feb\u901f\u7406\u89e3\u51fd\u6570\u7684\u9884\u671f\u884c\u4e3a\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528\u4f8b\u5b50\u5c55\u793a\u65e0\u8fd4\u56de\u503c\u7684\u5e94\u7528\u573a\u666f<\/p>\n<\/p>\n<ol>\n<li>\u6253\u5370\u4fe1\u606f\u7684\u51fd\u6570<\/li>\n<\/ol>\n<p><p>\u6709\u4e9b\u51fd\u6570\u4e3b\u8981\u7528\u4e8e\u6253\u5370\u4fe1\u606f\uff0c\u800c\u4e0d\u9700\u8981\u8fd4\u56de\u4efb\u4f55\u503c\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528<code>None<\/code>\u6216\u7701\u7565<code>return<\/code>\u8bed\u53e5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def print_message(message):<\/p>\n<p>    print(message)<\/p>\n<p>    return None<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4fee\u6539\u5168\u5c40\u53d8\u91cf\u7684\u51fd\u6570<\/li>\n<\/ol>\n<p><p>\u6709\u4e9b\u51fd\u6570\u7528\u4e8e\u4fee\u6539\u5168\u5c40\u53d8\u91cf\u6216\u5bf9\u8c61\u7684\u72b6\u6001\uff0c\u800c\u4e0d\u9700\u8981\u8fd4\u56de\u4efb\u4f55\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">counter = 0<\/p>\n<p>def increment_counter():<\/p>\n<p>    global counter<\/p>\n<p>    counter += 1<\/p>\n<p>    return None<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li>\u56de\u8c03\u51fd\u6570<\/li>\n<\/ol>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u56de\u8c03\u51fd\u6570\u88ab\u8bbe\u8ba1\u4e3a\u4e0d\u8fd4\u56de\u503c\u3002\u4f8b\u5982\uff0c\u5728\u4e8b\u4ef6\u9a71\u52a8\u7684\u7f16\u7a0b\u6a21\u578b\u4e2d\uff0c\u56de\u8c03\u51fd\u6570\u901a\u5e38\u7528\u4e8e\u54cd\u5e94\u4e8b\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def on_event(event):<\/p>\n<p>    print(f&quot;Event received: {event}&quot;)<\/p>\n<p>    return None<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u8868\u793a\u65e0\u8fd4\u56de\u503c\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u6700\u5e38\u89c1\u7684\u662f\u4f7f\u7528<code>None<\/code>\u548c\u901a\u8fc7\u7701\u7565<code>return<\/code>\u8bed\u53e5\u3002\u6839\u636e\u5177\u4f53\u60c5\u51b5\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u5728\u51fd\u6570\u7b7e\u540d\u4e2d\u4f7f\u7528\u6ce8\u91ca\u6216\u7c7b\u578b\u63d0\u793a\uff0c\u4e5f\u662f\u4e00\u79cd\u660e\u786e\u8868\u793a\u51fd\u6570\u6ca1\u6709\u8fd4\u56de\u503c\u7684\u597d\u65b9\u6cd5\u3002\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u5f0f\uff0c\u90fd\u5e94\u8be5\u9075\u5faa\u4ee3\u7801\u7684\u98ce\u683c\u548c\u56e2\u961f\u7684\u7f16\u7801\u89c4\u8303\uff0c\u4ee5\u786e\u4fdd\u4ee3\u7801\u7684\u4e00\u81f4\u6027\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u5b9a\u4e49\u4e00\u4e2a\u6ca1\u6709\u8fd4\u56de\u503c\u7684\u51fd\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u800c\u4e0d\u4f7f\u7528<code>return<\/code>\u8bed\u53e5\u6765\u8868\u793a\u8be5\u51fd\u6570\u6ca1\u6709\u8fd4\u56de\u503c\u3002\u8fd9\u6837\u7684\u51fd\u6570\u5728\u6267\u884c\u5b8c\u5176\u4ee3\u7801\u5757\u540e\u4f1a\u81ea\u52a8\u8fd4\u56de<code>None<\/code>\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">def my_function():\n    print(&quot;This function does not return a value.&quot;)\n<\/code><\/pre>\n<p>\u8c03\u7528<code>my_function()<\/code>\u65f6\uff0c\u5b83\u4f1a\u8f93\u51fa\u4fe1\u606f\uff0c\u4f46\u6ca1\u6709\u8fd4\u56de\u4efb\u4f55\u5177\u4f53\u503c\u3002<\/p>\n<p><strong>\u65e0\u8fd4\u56de\u503c\u7684\u51fd\u6570\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u6709\u4ec0\u4e48\u7528\u5904\uff1f<\/strong><br \/>\u5c3d\u7ba1\u65e0\u8fd4\u56de\u503c\u7684\u51fd\u6570\u4e0d\u4f1a\u8fd4\u56de\u6570\u636e\uff0c\u4f46\u5b83\u4eec\u5728\u6267\u884c\u7279\u5b9a\u4efb\u52a1\u65f6\u4ecd\u7136\u975e\u5e38\u6709\u7528\u3002\u4f8b\u5982\uff0c\u5904\u7406\u6587\u4ef6\u3001\u6253\u5370\u8f93\u51fa\u3001\u66f4\u65b0\u6570\u636e\u5e93\u6216\u8fdb\u884c\u6570\u636e\u9a8c\u8bc1\u7b49\u64cd\u4f5c\u90fd\u53ef\u4ee5\u901a\u8fc7\u65e0\u8fd4\u56de\u503c\u7684\u51fd\u6570\u6765\u5b9e\u73b0\u3002\u8fd9\u4e9b\u51fd\u6570\u7684\u4e3b\u8981\u76ee\u7684\u662f\u6267\u884c\u67d0\u9879\u64cd\u4f5c\uff0c\u800c\u4e0d\u662f\u8fd4\u56de\u7ed3\u679c\u3002<\/p>\n<p><strong>\u5982\u4f55\u68c0\u67e5\u4e00\u4e2a\u51fd\u6570\u662f\u5426\u8fd4\u56de\u503c\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528\u51fd\u6570\u5e76\u4f7f\u7528<code>type()<\/code>\u6216\u7b80\u5355\u7684<code>print()<\/code>\u8bed\u53e5\u6765\u68c0\u67e5\u5176\u8fd4\u56de\u503c\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4e00\u4e2a\u51fd\u6570\u6ca1\u6709\u8fd4\u56de\u503c\uff0c\u8c03\u7528\u5b83\u540e\u5c06\u5f97\u5230<code>None<\/code>\uff1a  <\/p>\n<pre><code class=\"language-python\">result = my_function()\nprint(type(result))  # \u8f93\u51fa\uff1a&lt;class &#39;NoneType&#39;&gt;\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u786e\u8ba4\u51fd\u6570\u884c\u4e3a\u5e76\u8c03\u8bd5\u4ee3\u7801\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u8868\u793a\u65e0\u8fd4\u56de\u503c\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4f7f\u7528None\u3001\u901a\u8fc7\u7701\u7565return\u8bed\u53e5\u3002\u5728Python\u4e2d\uff0c\u51fd\u6570\u9ed8\u8ba4 [&hellip;]","protected":false},"author":3,"featured_media":1037080,"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\/1037072"}],"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=1037072"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1037072\/revisions"}],"predecessor-version":[{"id":1037082,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1037072\/revisions\/1037082"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1037080"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1037072"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1037072"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1037072"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}