{"id":998771,"date":"2024-12-27T09:35:52","date_gmt":"2024-12-27T01:35:52","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/998771.html"},"modified":"2024-12-27T09:35:54","modified_gmt":"2024-12-27T01:35:54","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e8%ae%be%e7%bd%ae%e5%b8%b8%e9%87%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/998771.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u8bbe\u7f6e\u5e38\u91cf"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25074052\/2b98389d-ecee-43cc-ae2a-e1317c797e86.webp\" alt=\"python\u4e2d\u5982\u4f55\u8bbe\u7f6e\u5e38\u91cf\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u6ca1\u6709\u5185\u7f6e\u7684\u8bed\u6cd5\u6765\u5b9a\u4e49\u771f\u6b63\u7684\u5e38\u91cf\uff0c\u4f46\u901a\u8fc7\u67d0\u4e9b\u7f16\u7a0b\u60ef\u4f8b\u548c\u6a21\u5757\uff0c\u53ef\u4ee5\u5b9e\u73b0\u7c7b\u4f3c\u5e38\u91cf\u7684\u884c\u4e3a\u3002<strong>\u4f7f\u7528\u5927\u5199\u5b57\u6bcd\u547d\u540d\u53d8\u91cf\u3001\u5229\u7528\u547d\u540d\u5143\u7ec4\u6216\u51bb\u7ed3\u96c6\u5408\u3001\u521b\u5efa\u5e38\u91cf\u7c7b\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u5982 <code>constantly<\/code><\/strong>\uff0c\u662f\u5e38\u89c1\u7684\u65b9\u6cd5\u3002\u5176\u4e2d\uff0c\u4f7f\u7528\u5927\u5199\u5b57\u6bcd\u547d\u540d\u53d8\u91cf\u662f\u6700\u5e7f\u6cdb\u5e94\u7528\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u7b80\u5355\u6613\u61c2\uff0c\u5e76\u4e0ePython\u7684\u60ef\u4f8b\u4e00\u81f4\u3002\u8fd9\u79cd\u65b9\u5f0f\u901a\u8fc7\u4e3a\u5e38\u91cf\u540d\u4f7f\u7528\u5168\u5927\u5199\u5b57\u6bcd\u6765\u63d0\u9192\u5f00\u53d1\u8005\uff0c\u8fd9\u4e9b\u53d8\u91cf\u7684\u503c\u4e0d\u5e94\u88ab\u4fee\u6539\u3002\u867d\u7136Python\u672c\u8eab\u4e0d\u80fd\u5f3a\u5236\u6267\u884c\u8fd9\u4e00\u70b9\uff0c\u4f46\u5927\u591a\u6570\u5f00\u53d1\u8005\u90fd\u4f1a\u5c0a\u91cd\u8fd9\u4e00\u60ef\u4f8b\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5927\u5199\u5b57\u6bcd\u547d\u540d\u53d8\u91cf<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u901a\u5e38\u901a\u8fc7\u5168\u5927\u5199\u5b57\u6bcd\u547d\u540d\u53d8\u91cf\u6765\u6807\u8bc6\u5e38\u91cf\u3002\u8fd9\u662f\u4e00\u79cd\u7ea6\u5b9a\u4fd7\u6210\u7684\u65b9\u6cd5\uff0c\u867d\u7136Python\u4e0d\u4f1a\u5f3a\u5236\u6267\u884c\uff0c\u4f46\u5f00\u53d1\u8005\u4eec\u666e\u904d\u9075\u5faa\u8fd9\u4e00\u7ea6\u5b9a\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u5e38\u91cf\u7684\u610f\u4e49\u548c\u7528\u9014\u5728\u4ee3\u7801\u4e2d\u4e00\u76ee\u4e86\u7136\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">PI = 3.14159<\/p>\n<p>GRAVITY = 9.8<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u5c06\u53d8\u91cf\u540d\u5168\u90e8\u5927\u5199\uff0c\u53ef\u4ee5\u63d0\u9192\u5176\u4ed6\u5f00\u53d1\u8005\u8fd9\u4e9b\u503c\u662f\u5e38\u91cf\uff0c\u5e94\u8be5\u4fdd\u6301\u4e0d\u53d8\u3002\u5728<a href=\"https:\/\/docs.pingcode.com\/code-review\" target=\"_blank\">\u4ee3\u7801\u5ba1\u67e5<\/a>\u8fc7\u7a0b\u4e2d\uff0c\u5982\u679c\u53d1\u73b0\u8fd9\u4e9b\u5e38\u91cf\u88ab\u4fee\u6539\uff0c\u4e5f\u4f1a\u5f15\u8d77\u6ce8\u610f\u3002\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u4e14\u76f4\u63a5\uff0c\u662f\u5927\u591a\u6570Python\u5f00\u53d1\u8005\u7684\u9996\u9009\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u547d\u540d\u5143\u7ec4\u6216\u51bb\u7ed3\u96c6\u5408<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u786e\u4fdd\u5e38\u91cf\u7684\u4e0d\u53ef\u53d8\u6027\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>collections<\/code>\u6a21\u5757\u4e2d\u7684<code>namedtuple<\/code>\u6216<code>frozenset<\/code>\u3002\u8fd9\u4e24\u79cd\u7ed3\u6784\u90fd\u63d0\u4f9b\u4e86\u4e00\u79cd\u65b9\u6cd5\u6765\u5b58\u50a8\u4e0d\u53ef\u53d8\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>1. \u547d\u540d\u5143\u7ec4<\/h4>\n<\/p>\n<p><p>\u547d\u540d\u5143\u7ec4\u662fPython\u4e2d\u4e00\u79cd\u975e\u5e38\u6709\u7528\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5141\u8bb8\u6211\u4eec\u4e3a\u6570\u636e\u63d0\u4f9b\u660e\u786e\u7684\u540d\u5b57\uff0c\u540c\u65f6\u4fdd\u6301\u5143\u7ec4\u7684\u4e0d\u53ef\u53d8\u6027\u3002<\/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.8)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u4f7f\u4ee3\u7801\u66f4\u5177\u53ef\u8bfb\u6027\uff0c\u8fd8\u80fd\u9632\u6b62\u5bf9\u5e38\u91cf\u503c\u7684\u610f\u5916\u66f4\u6539\u3002<\/p>\n<\/p>\n<p><h4>2. \u51bb\u7ed3\u96c6\u5408<\/h4>\n<\/p>\n<p><p>\u51bb\u7ed3\u96c6\u5408\u662f\u96c6\u5408\u7684\u4e0d\u53ef\u53d8\u7248\u672c\uff0c\u9002\u7528\u4e8e\u9700\u8981\u5b58\u50a8\u552f\u4e00\u503c\u96c6\u5408\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">constants_set = frozenset({&#39;PI&#39;: 3.14159, &#39;GRAVITY&#39;: 9.8})<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u867d\u7136\u51bb\u7ed3\u96c6\u5408\u4e0d\u80fd\u50cf\u5b57\u5178\u90a3\u6837\u901a\u8fc7\u952e\u503c\u5bf9\u5b58\u50a8\u6570\u636e\uff0c\u4f46\u5b83\u5728\u9700\u8981\u4e0d\u53ef\u53d8\u96c6\u5408\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u521b\u5efa\u5e38\u91cf\u7c7b<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a\u7c7b\u6765\u5b58\u50a8\u5e38\u91cf\uff0c\u5e76\u5c06\u6240\u6709\u5e38\u91cf\u5b9a\u4e49\u4e3a\u7c7b\u7684\u5c5e\u6027\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u7ec4\u7ec7\u826f\u597d\uff0c\u800c\u4e14\u53ef\u4ee5\u901a\u8fc7\u7c7b\u540d\u6e05\u695a\u5730\u5f15\u7528\u5e38\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Constants:<\/p>\n<p>    PI = 3.14159<\/p>\n<p>    GRAVITY = 9.8<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u597d\u5904\u5728\u4e8e\uff0c\u6240\u6709\u5e38\u91cf\u90fd\u96c6\u4e2d\u5728\u4e00\u4e2a\u5730\u65b9\uff0c\u4fbf\u4e8e\u7ba1\u7406\u548c\u7ef4\u62a4\u3002\u6b64\u5916\uff0c\u901a\u8fc7\u4f7f\u7528\u7c7b\u7684\u5c5e\u6027\uff0c\u53ef\u4ee5\u907f\u514d\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u7684\u6c61\u67d3\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u9700\u8981\u66f4\u4e25\u683c\u5e38\u91cf\u884c\u4e3a\u7684\u9879\u76ee\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u3002\u4f8b\u5982\uff0c<code>constantly<\/code>\u662f\u4e00\u4e2a\u63d0\u4f9b\u5e38\u91cf\u652f\u6301\u7684Python\u5e93\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5\u5e93<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u5b89\u88c5\u4e86<code>constantly<\/code>\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install constantly<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u5e93<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>constantly<\/code>\u5e93\uff0c\u53ef\u4ee5\u5b9a\u4e49\u771f\u6b63\u7684\u5e38\u91cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from constantly import NamedConstant<\/p>\n<p>class Constants:<\/p>\n<p>    PI = NamedConstant()<\/p>\n<p>    GRAVITY = NamedConstant()<\/p>\n<p>constants = Constants()<\/p>\n<p>constants.PI  # \u4f7f\u7528\u5e38\u91cf<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u63d0\u4f9b\u4e86\u66f4\u4e25\u683c\u7684\u5e38\u91cf\u5b9a\u4e49\uff0c\u5e76\u9632\u6b62\u610f\u5916\u4fee\u6539\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u9075\u5faaPython\u7684\u60ef\u4f8b<\/h3>\n<\/p>\n<p><p>\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u5728\u4f7f\u7528\u5e38\u91cf\u65f6\uff0c\u59cb\u7ec8\u9075\u5faaPython\u7684\u547d\u540d\u60ef\u4f8b\u548c\u4ee3\u7801\u98ce\u683c\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u8fd9\u4e0d\u4ec5\u6709\u52a9\u4e8e\u4fdd\u6301\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\uff0c\u8fd8\u80fd\u786e\u4fdd\u56e2\u961f\u6210\u5458\u4e4b\u95f4\u7684\u534f\u4f5c\u66f4\u52a0\u987a\u7545\u3002<\/p>\n<\/p>\n<p><h4>1. \u547d\u540d\u60ef\u4f8b<\/h4>\n<\/p>\n<p><p>\u9075\u5faaPEP 8\u547d\u540d\u7ea6\u5b9a\uff0c\u4f7f\u7528\u5168\u5927\u5199\u5b57\u6bcd\u8868\u793a\u5e38\u91cf\uff0c\u4f8b\u5982<code>MAX_CONNECTIONS<\/code>\u3001<code>DEFAULT_TIMEOUT<\/code>\u7b49\u3002<\/p>\n<\/p>\n<p><h4>2. \u6587\u6863\u5316\u5e38\u91cf<\/h4>\n<\/p>\n<p><p>\u5728\u4ee3\u7801\u4e2d\u6dfb\u52a0\u9002\u5f53\u7684\u6ce8\u91ca\u548c\u6587\u6863\uff0c\u4ee5\u89e3\u91ca\u5e38\u91cf\u7684\u7528\u9014\u548c\u610f\u4e49\u3002\u8fd9\u6709\u52a9\u4e8e\u65b0\u52a0\u5165\u7684\u5f00\u53d1\u8005\u5feb\u901f\u7406\u89e3\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u9075\u5faa\u8fd9\u4e9b\u6700\u4f73\u5b9e\u8df5\uff0c\u53ef\u4ee5\u5728Python\u4e2d\u6709\u6548\u5730\u4f7f\u7528\u5e38\u91cf\uff0c\u786e\u4fdd\u4ee3\u7801\u7684\u7a33\u5b9a\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u5728\u590d\u6742\u9879\u76ee\u4e2d\uff0c\u5408\u7406\u7ba1\u7406\u5e38\u91cf\u53ef\u4ee5\u51cf\u5c11\u9519\u8bef\uff0c\u63d0\u9ad8\u4ee3\u7801\u8d28\u91cf\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5e38\u91cf\u5728Python\u4e2d\u662f\u5982\u4f55\u5b9a\u4e49\u548c\u4f7f\u7528\u7684\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5e38\u91cf\u901a\u5e38\u4e0d\u662f\u901a\u8fc7\u8bed\u8a00\u7279\u6027\u76f4\u63a5\u652f\u6301\u7684\uff0c\u800c\u662f\u901a\u8fc7\u547d\u540d\u7ea6\u5b9a\u6765\u5b9e\u73b0\u3002\u5f00\u53d1\u8005\u901a\u5e38\u4f7f\u7528\u5168\u5927\u5199\u5b57\u6bcd\u6765\u547d\u540d\u5e38\u91cf\uff0c\u4f8b\u5982<code>PI = 3.14<\/code>\u3002\u867d\u7136\u8fd9\u5e76\u4e0d\u80fd\u9632\u6b62\u53d8\u91cf\u88ab\u4fee\u6539\uff0c\u4f46\u5b83\u662f\u4e00\u79cd\u7ea6\u5b9a\uff0c\u63d0\u9192\u7a0b\u5e8f\u5458\u8be5\u503c\u4e0d\u5e94\u88ab\u6539\u53d8\u3002<\/p>\n<p><strong>Python\u4e2d\u5e38\u91cf\u7684\u6700\u4f73\u5b9e\u8df5\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>\u4e3a\u4e86\u786e\u4fdd\u5e38\u91cf\u7684\u4f7f\u7528\u66f4\u52a0\u660e\u786e\uff0c\u5efa\u8bae\u5728\u6a21\u5757\u9876\u90e8\u5b9a\u4e49\u6240\u6709\u5e38\u91cf\uff0c\u5e76\u4f7f\u7528\u5168\u5927\u5199\u7684\u547d\u540d\u98ce\u683c\u3002\u6b64\u5916\uff0c\u53ef\u4ee5\u5c06\u5e38\u91cf\u653e\u5728\u4e00\u4e2a\u5355\u72ec\u7684\u914d\u7f6e\u6587\u4ef6\u4e2d\uff0c\u4ee5\u4fbf\u4e8e\u7ba1\u7406\u548c\u7ef4\u62a4\u3002\u786e\u4fdd\u5728\u4ee3\u7801\u4e2d\u5c3d\u91cf\u907f\u514d\u76f4\u63a5\u4fee\u6539\u8fd9\u4e9b\u5e38\u91cf\u7684\u503c\uff0c\u6709\u52a9\u4e8e\u4fdd\u6301\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u7a33\u5b9a\u6027\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u5e38\u91cf\u4e0d\u53ef\u53d8\u7684\u7279\u6027\uff1f<\/strong><br \/>\u867d\u7136Python\u6ca1\u6709\u5185\u5efa\u7684\u5e38\u91cf\u652f\u6301\uff0c\u4f46\u53ef\u4ee5\u901a\u8fc7\u81ea\u5b9a\u4e49\u7c7b\u6216\u8005\u4f7f\u7528<code>namedtuple<\/code>\u6765\u6a21\u62df\u5e38\u91cf\u7684\u4e0d\u53ef\u53d8\u6027\u3002\u521b\u5efa\u4e00\u4e2a\u7c7b\u5e76\u91cd\u5199<code>__setattr__<\/code>\u65b9\u6cd5\uff0c\u6765\u963b\u6b62\u5bf9\u5c5e\u6027\u7684\u4fee\u6539\uff0c\u6216\u8005\u4f7f\u7528<code>frozenset<\/code>\u6765\u521b\u5efa\u4e0d\u53ef\u53d8\u96c6\u5408\uff0c\u90fd\u662f\u4e0d\u9519\u7684\u9009\u62e9\u3002\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u589e\u52a0\u4ee3\u7801\u7684\u5b89\u5168\u6027\uff0c\u9632\u6b62\u610f\u5916\u7684\u503c\u66f4\u6539\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u6ca1\u6709\u5185\u7f6e\u7684\u8bed\u6cd5\u6765\u5b9a\u4e49\u771f\u6b63\u7684\u5e38\u91cf\uff0c\u4f46\u901a\u8fc7\u67d0\u4e9b\u7f16\u7a0b\u60ef\u4f8b\u548c\u6a21\u5757\uff0c\u53ef\u4ee5\u5b9e\u73b0\u7c7b\u4f3c\u5e38\u91cf\u7684\u884c\u4e3a\u3002\u4f7f\u7528\u5927\u5199\u5b57 [&hellip;]","protected":false},"author":3,"featured_media":998782,"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\/998771"}],"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=998771"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/998771\/revisions"}],"predecessor-version":[{"id":998783,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/998771\/revisions\/998783"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/998782"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=998771"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=998771"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=998771"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}