{"id":967870,"date":"2024-12-27T05:01:21","date_gmt":"2024-12-26T21:01:21","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/967870.html"},"modified":"2024-12-27T05:01:23","modified_gmt":"2024-12-26T21:01:23","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%ae%9a%e4%b9%89%e5%b8%b8%e9%87%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/967870.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u5b9a\u4e49\u5e38\u91cf"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24182938\/0c0f8224-1f56-4d7c-9d26-9aecf5c07dc1.webp\" alt=\"python\u4e2d\u5982\u4f55\u5b9a\u4e49\u5e38\u91cf\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u5b9a\u4e49\u5e38\u91cf\u901a\u5e38\u4f7f\u7528\u5927\u5199\u5b57\u6bcd\u547d\u540d\u3001\u901a\u8fc7\u6a21\u5757\u6216\u7c7b\u5b9a\u4e49\u3001\u901a\u8fc7<code>collections.namedtuple<\/code>\u5b9e\u73b0\u3002<\/strong> Python\u672c\u8eab\u6ca1\u6709\u5185\u7f6e\u5e38\u91cf\u7c7b\u578b\uff0c\u4f46\u53ef\u4ee5\u901a\u8fc7\u7f16\u7801\u7ea6\u5b9a\u6765\u6a21\u62df\u5e38\u91cf\u3002\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u5168\u5927\u5199\u7684\u53d8\u91cf\u540d\u6765\u8868\u793a\u5e38\u91cf\uff0c\u5c3d\u7ba1\u8fd9\u79cd\u505a\u6cd5\u4f9d\u8d56\u4e8e\u7a0b\u5e8f\u5458\u7684\u81ea\u5f8b\uff0c\u56e0\u4e3aPython\u8bed\u8a00\u672c\u8eab\u4e0d\u4f1a\u963b\u6b62\u5bf9\u8fd9\u4e9b\u53d8\u91cf\u7684\u91cd\u65b0\u8d4b\u503c\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u6a21\u5757\u6216\u7c7b\u6765\u5c01\u88c5\u5e38\u91cf\uff0c\u6216\u8005\u4f7f\u7528<code>collections.namedtuple<\/code>\u6765\u6a21\u62df\u5e38\u91cf\u7684\u884c\u4e3a\u3002\u4ee5\u4e0b\u662f\u5bf9\u8fd9\u4e9b\u65b9\u6cd5\u7684\u8be6\u7ec6\u4ecb\u7ecd\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u5927\u5199\u5b57\u6bcd\u547d\u540d<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u6700\u5e38\u89c1\u7684\u5b9a\u4e49\u5e38\u91cf\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u5168\u5927\u5199\u5b57\u6bcd\u7684\u53d8\u91cf\u540d\u3002\u8fd9\u79cd\u65b9\u6cd5\u662f\u4e00\u79cd\u7ea6\u5b9a\uff0c\u65e8\u5728\u63d0\u9192\u7a0b\u5e8f\u5458\u8fd9\u4e9b\u53d8\u91cf\u4e0d\u5e94\u88ab\u4fee\u6539\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">PI = 3.14159<\/p>\n<p>GRAVITY = 9.81<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u660e\u4e86\uff0c\u9002\u5408\u5b9a\u4e49\u7b80\u5355\u7684\u3001\u5c11\u91cf\u7684\u5e38\u91cf\u3002\u4f46\u662f\uff0c\u5b83\u4f9d\u8d56\u4e8e\u5f00\u53d1\u8005\u7684\u81ea\u89c9\u6027\uff0c\u56e0\u4e3aPython\u5e76\u6ca1\u6709\u673a\u5236\u963b\u6b62\u91cd\u65b0\u8d4b\u503c\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u901a\u8fc7\u6a21\u5757\u6216\u7c7b\u5b9a\u4e49<\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u597d\u5730\u7ec4\u7ec7\u5e38\u91cf\uff0c\u5e76\u4e3a\u5176\u63d0\u4f9b\u4e00\u5b9a\u7684\u201c\u4fdd\u62a4\u201d\uff0c\u53ef\u4ee5\u5c06\u5e38\u91cf\u5b9a\u4e49\u5728\u6a21\u5757\u6216\u7c7b\u4e2d\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6a21\u5757\u7ea7\u5e38\u91cf<\/strong><\/p>\n<\/p>\n<p><p>\u5c06\u5e38\u91cf\u653e\u5728\u5355\u72ec\u7684Python\u6a21\u5757\u4e2d\uff0c\u7136\u540e\u5728\u9700\u8981\u4f7f\u7528\u5e38\u91cf\u7684\u5176\u4ed6\u6a21\u5757\u4e2d\u5bfc\u5165\u8fd9\u4e9b\u5e38\u91cf\u3002\u8fd9\u4e0d\u4ec5\u6709\u52a9\u4e8e\u7ec4\u7ec7\u4ee3\u7801\uff0c\u8fd8\u53ef\u4ee5\u9632\u6b62\u5e38\u91cf\u88ab\u610f\u5916\u4fee\u6539\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># constants.py<\/p>\n<p>PI = 3.14159<\/p>\n<p>GRAVITY = 9.81<\/p>\n<h2><strong>m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n.py<\/strong><\/h2>\n<p>import constants<\/p>\n<p>print(constants.PI)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7c7b\u7ea7\u5e38\u91cf<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u7c7b\u5c06\u5e38\u91cf\u5c01\u88c5\u5728\u4e00\u8d77\uff0c\u8fd9\u6837\u53ef\u4ee5\u901a\u8fc7\u7c7b\u540d\u8bbf\u95ee\u5e38\u91cf\uff0c\u540c\u65f6\u4e5f\u4e3a\u5e38\u91cf\u63d0\u4f9b\u4e86\u903b\u8f91\u4e0a\u7684\u5f52\u5c5e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Constants:<\/p>\n<p>    PI = 3.14159<\/p>\n<p>    GRAVITY = 9.81<\/p>\n<p>print(Constants.PI)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528\u7c7b\u6765\u5b9a\u4e49\u5e38\u91cf\u7684\u597d\u5904\u5728\u4e8e\u53ef\u4ee5\u901a\u8fc7\u7ee7\u627f\u6765\u6269\u5c55\u5e38\u91cf\u96c6\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u901a\u8fc7<code>collections.namedtuple<\/code>\u5b9e\u73b0<\/p>\n<\/p>\n<p><p><code>collections.namedtuple<\/code>\u662fPython\u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u4e2a\u5de5\u5382\u51fd\u6570\uff0c\u7528\u4e8e\u521b\u5efa\u4e0d\u53ef\u53d8\u7684\u547d\u540d\u5143\u7ec4\u3002\u53ef\u4ee5\u7528\u5b83\u6765\u6a21\u62df\u5e38\u91cf\uff0c\u56e0\u4e3a\u547d\u540d\u5143\u7ec4\u7684\u5b57\u6bb5\u662f\u4e0d\u53ef\u53d8\u7684\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import namedtuple<\/p>\n<p>Constants = namedtuple(&#39;Constants&#39;, [&#39;PI&#39;, &#39;GRAVITY&#39;])<\/p>\n<p>constants = Constants(PI=3.14159, GRAVITY=9.81)<\/p>\n<p>print(constants.PI)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u4f7f\u5e38\u91cf\u4e0d\u53ef\u53d8\uff0c\u8fd8\u5141\u8bb8\u5bf9\u5e38\u91cf\u8fdb\u884c\u547d\u540d\u8bbf\u95ee\uff0c\u63d0\u4f9b\u4e86\u7c7b\u4f3c\u4e8e\u5bf9\u8c61\u5c5e\u6027\u7684\u8bed\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528<code>enum.Enum<\/code><\/p>\n<\/p>\n<p><p>\u867d\u7136<code>enum<\/code>\u901a\u5e38\u7528\u4e8e\u5b9a\u4e49\u679a\u4e3e\u7c7b\u578b\uff0c\u4f46\u4e5f\u53ef\u4ee5\u7528\u6765\u5b9a\u4e49\u4e00\u7ec4\u76f8\u5173\u7684\u5e38\u91cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from enum import Enum<\/p>\n<p>class Constants(Enum):<\/p>\n<p>    PI = 3.14159<\/p>\n<p>    GRAVITY = 9.81<\/p>\n<p>print(Constants.PI.value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528<code>enum.Enum<\/code>\u53ef\u4ee5\u786e\u4fdd\u5e38\u91cf\u503c\u7684\u552f\u4e00\u6027\uff0c\u5e76\u9632\u6b62\u610f\u5916\u4fee\u6539\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u901a\u8fc7\u5916\u90e8\u5e93<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u5229\u7528\u5916\u90e8\u5e93\u6765\u5b9e\u73b0\u5e38\u91cf\u7684\u5b9a\u4e49\u548c\u7ba1\u7406\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>environs<\/code>\u5e93\u6765\u52a0\u8f7d\u548c\u7ba1\u7406\u73af\u5883\u53d8\u91cf\u4f5c\u4e3a\u5e38\u91cf\u3002<\/p>\n<\/p>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0c\u867d\u7136Python\u6ca1\u6709\u5185\u7f6e\u7684\u5e38\u91cf\u673a\u5236\uff0c\u4f46\u901a\u8fc7\u7f16\u7801\u7ea6\u5b9a\u548c\u4e00\u4e9b\u6280\u5de7\uff0c\u6211\u4eec\u53ef\u4ee5\u6709\u6548\u5730\u6a21\u62df\u5e38\u91cf\u7684\u884c\u4e3a\u3002\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u4ee5\u53ca\u4e2a\u4eba\u7f16\u7801\u4e60\u60ef\u3002\u65e0\u8bba\u91c7\u7528\u54ea\u79cd\u65b9\u5f0f\uff0c\u786e\u4fdd\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u59cb\u7ec8\u662f\u6700\u91cd\u8981\u7684\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0c\u5e38\u91cf\u7684\u547d\u540d\u7ea6\u5b9a\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5e38\u91cf\u901a\u5e38\u4f7f\u7528\u5168\u5927\u5199\u5b57\u6bcd\u6765\u547d\u540d\uff0c\u4ee5\u533a\u5206\u4e8e\u53d8\u91cf\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>MAX_VALUE<\/code>\u6216<code>PI<\/code>\u7b49\u5f62\u5f0f\u6765\u8868\u793a\u5e38\u91cf\u3002\u8fd9\u79cd\u547d\u540d\u7ea6\u5b9a\u5e2e\u52a9\u5f00\u53d1\u8005\u8bc6\u522b\u8fd9\u4e9b\u503c\u4e0d\u5e94\u8be5\u88ab\u4fee\u6539\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5b9a\u4e49\u5e38\u91cf\u65f6\uff0c\u662f\u5426\u6709\u7279\u5b9a\u7684\u6a21\u5757\u6216\u5e93\u63a8\u8350\uff1f<\/strong><br \/>\u867d\u7136Python\u6ca1\u6709\u5185\u7f6e\u7684\u5e38\u91cf\u7c7b\u578b\uff0c\u4f46\u4f7f\u7528<code>enum<\/code>\u6a21\u5757\u53ef\u4ee5\u5e2e\u52a9\u5b9a\u4e49\u4e00\u7ec4\u5e38\u91cf\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a\u679a\u4e3e\u7c7b\u6765\u5b9a\u4e49\u5e38\u91cf\u503c\uff0c\u8fd9\u6837\u53ef\u4ee5\u66f4\u597d\u5730\u7ec4\u7ec7\u548c\u7ba1\u7406\u8fd9\u4e9b\u5e38\u91cf\u3002<\/p>\n<p><strong>\u5982\u4f55\u786e\u4fdd\u5e38\u91cf\u4e0d\u88ab\u610f\u5916\u4fee\u6539\uff1f<\/strong><br \/>\u867d\u7136Python\u4e0d\u5f3a\u5236\u4fdd\u62a4\u5e38\u91cf\uff0c\u4f46\u53ef\u4ee5\u901a\u8fc7\u9075\u5faa\u4ee3\u7801\u7ea6\u5b9a\u6765\u51cf\u5c11\u610f\u5916\u4fee\u6539\u7684\u53ef\u80fd\u6027\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5728\u4ee3\u7801\u4e2d\u6dfb\u52a0\u6ce8\u91ca\uff0c\u8bf4\u660e\u67d0\u4e9b\u53d8\u91cf\u662f\u5e38\u91cf\uff0c\u6216\u8005\u901a\u8fc7\u4f7f\u7528\u547d\u540d\u7ea6\u5b9a\u6765\u63d0\u9192\u5176\u4ed6\u5f00\u53d1\u8005\u3002\u6b64\u5916\uff0c\u4f7f\u7528<code>property<\/code>\u88c5\u9970\u5668\u6765\u5c01\u88c5\u53d8\u91cf\u4e5f\u662f\u4e00\u79cd\u6709\u6548\u7684\u65b9\u5f0f\uff0c\u80fd\u591f\u5728\u4e00\u5b9a\u7a0b\u5ea6\u4e0a\u9632\u6b62\u5176\u88ab\u6539\u53d8\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5b9a\u4e49\u5e38\u91cf\u901a\u5e38\u4f7f\u7528\u5927\u5199\u5b57\u6bcd\u547d\u540d\u3001\u901a\u8fc7\u6a21\u5757\u6216\u7c7b\u5b9a\u4e49\u3001\u901a\u8fc7collections.namedtupl [&hellip;]","protected":false},"author":3,"featured_media":967873,"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\/967870"}],"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=967870"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/967870\/revisions"}],"predecessor-version":[{"id":967877,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/967870\/revisions\/967877"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/967873"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=967870"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=967870"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=967870"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}