{"id":944581,"date":"2024-12-26T23:09:32","date_gmt":"2024-12-26T15:09:32","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/944581.html"},"modified":"2024-12-26T23:09:34","modified_gmt":"2024-12-26T15:09:34","slug":"python%e5%a6%82%e4%bd%95%e5%8a%a0%e5%90%8d%e5%ad%97","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/944581.html","title":{"rendered":"python\u5982\u4f55\u52a0\u540d\u5b57"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25081524\/56cd0bcf-7ad2-4b22-b999-0377f78501fc.webp\" alt=\"python\u5982\u4f55\u52a0\u540d\u5b57\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u4e3a\u53d8\u91cf\u6216\u5bf9\u8c61\u6dfb\u52a0\u540d\u5b57\u7684\u65b9\u5f0f\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u53d8\u91cf\u547d\u540d\u3001\u521b\u5efa\u7c7b\u548c\u5bf9\u8c61\u5c5e\u6027\u3001\u4f7f\u7528\u5b57\u5178\u5b58\u50a8\u540d\u79f0\u4e0e\u503c\u7684\u5bf9\u5e94\u5173\u7cfb\u3002<\/strong> \u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u901a\u8fc7\u53d8\u91cf\u547d\u540d\u76f4\u63a5\u4e3a\u6570\u636e\u8d4b\u4e88\u540d\u79f0\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728Python\u4e2d\u4e3a\u6570\u636e\u6216\u5bf9\u8c61\u6dfb\u52a0\u540d\u79f0\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e9b\u793a\u4f8b\u4ee3\u7801\u6765\u5e2e\u52a9\u7406\u89e3\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u53d8\u91cf\u547d\u540d<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6700\u7b80\u5355\u7684\u4e3a\u6570\u636e\u52a0\u540d\u5b57\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u53d8\u91cf\u547d\u540d\u3002\u901a\u8fc7\u5c06\u6570\u636e\u8d4b\u503c\u7ed9\u4e00\u4e2a\u53d8\u91cf\uff0c\u6211\u4eec\u53ef\u4ee5\u4e3a\u8be5\u6570\u636e\u63d0\u4f9b\u4e00\u4e2a\u6613\u4e8e\u8bc6\u522b\u548c\u4f7f\u7528\u7684\u540d\u79f0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4e3a\u4e00\u4e2a\u5b57\u7b26\u4e32\u6570\u636e\u6dfb\u52a0\u540d\u79f0<\/p>\n<p>name = &quot;John Doe&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u660e\u4e86\uff0c\u9002\u7528\u4e8e\u9700\u8981\u4e3a\u7b80\u5355\u6570\u636e\u7c7b\u578b\uff08\u5982\u6574\u6570\u3001\u6d6e\u70b9\u6570\u3001\u5b57\u7b26\u4e32\u7b49\uff09\u547d\u540d\u7684\u573a\u666f\u3002\u53d8\u91cf\u540d\u5e94\u5177\u6709\u63cf\u8ff0\u6027\uff0c\u4ee5\u4fbf\u4ee3\u7801\u66f4\u5177\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5b57\u5178\u5b58\u50a8\u540d\u79f0\u4e0e\u503c\u7684\u5bf9\u5e94\u5173\u7cfb<\/h3>\n<\/p>\n<p><p>\u5b57\u5178\u662fPython\u4e2d\u7684\u4e00\u79cd\u5185\u7f6e\u6570\u636e\u7ed3\u6784\uff0c\u7528\u4e8e\u5b58\u50a8\u952e\u503c\u5bf9\u3002\u901a\u8fc7\u5b57\u5178\uff0c\u6211\u4eec\u53ef\u4ee5\u4e3a\u591a\u4e2a\u6570\u636e\u9879\u6279\u91cf\u6dfb\u52a0\u540d\u79f0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u5b57\u5178\u4e3a\u591a\u4e2a\u540d\u79f0\u5bf9\u5e94\u591a\u4e2a\u503c<\/p>\n<p>person_info = {<\/p>\n<p>    &quot;name&quot;: &quot;John Doe&quot;,<\/p>\n<p>    &quot;age&quot;: 30,<\/p>\n<p>    &quot;occupation&quot;: &quot;Engineer&quot;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b57\u5178\u7684\u4f18\u52bf\u5728\u4e8e\uff0c\u5b83\u53ef\u4ee5\u9ad8\u6548\u5730\u5b58\u50a8\u548c\u67e5\u627e\u591a\u4e2a\u540d\u79f0\u4e0e\u503c\u7684\u5bf9\u5e94\u5173\u7cfb\uff0c\u975e\u5e38\u9002\u5408\u9700\u8981\u7ba1\u7406\u591a\u4e2a\u5c5e\u6027\u7684\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u521b\u5efa\u7c7b\u548c\u5bf9\u8c61\u5c5e\u6027<\/h3>\n<\/p>\n<p><p>\u5f53\u9700\u8981\u4e3a\u4e00\u4e2a\u5bf9\u8c61\u7684\u591a\u4e2a\u5c5e\u6027\u6dfb\u52a0\u540d\u79f0\u65f6\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u7c7b\uff0c\u5e76\u5728\u7c7b\u4e2d\u5b9a\u4e49\u5c5e\u6027\u3002\u7c7b\u548c\u5bf9\u8c61\u5c5e\u6027\u63d0\u4f9b\u4e86\u4e00\u79cd\u7ed3\u6784\u5316\u7684\u65b9\u5f0f\u6765\u7ba1\u7406\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u4e00\u4e2a\u7c7b\uff0c\u4e3a\u5bf9\u8c61\u5c5e\u6027\u6dfb\u52a0\u540d\u79f0<\/p>\n<p>class Person:<\/p>\n<p>    def __init__(self, name, age, occupation):<\/p>\n<p>        self.name = name<\/p>\n<p>        self.age = age<\/p>\n<p>        self.occupation = occupation<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5e76\u4e3a\u5176\u5c5e\u6027\u8d4b\u503c<\/strong><\/h2>\n<p>person = Person(&quot;John Doe&quot;, 30, &quot;Engineer&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528\u7c7b\u548c\u5bf9\u8c61\u5c5e\u6027\u7684\u597d\u5904\u662f\uff0c\u53ef\u4ee5\u5c06\u76f8\u5173\u7684\u6570\u636e\u548c\u884c\u4e3a\u5c01\u88c5\u5728\u4e00\u8d77\uff0c\u589e\u5f3a\u4ee3\u7801\u7684\u53ef\u6269\u5c55\u6027\u548c\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u547d\u540d\u5143\u7ec4<\/h3>\n<\/p>\n<p><p>\u547d\u540d\u5143\u7ec4\u662fPython collections\u6a21\u5757\u4e2d\u7684\u4e00\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u5b83\u5141\u8bb8\u4e3a\u5143\u7ec4\u7684\u6bcf\u4e2a\u5143\u7d20\u6dfb\u52a0\u540d\u79f0\uff0c\u4f7f\u4ee3\u7801\u66f4\u5177\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import namedtuple<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u547d\u540d\u5143\u7ec4\u7c7b\u578b<\/strong><\/h2>\n<p>Person = namedtuple(&#39;Person&#39;, [&#39;name&#39;, &#39;age&#39;, &#39;occupation&#39;])<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u547d\u540d\u5143\u7ec4\u5b9e\u4f8b<\/strong><\/h2>\n<p>person = Person(name=&quot;John Doe&quot;, age=30, occupation=&quot;Engineer&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u547d\u540d\u5143\u7ec4\u7684\u4f18\u70b9\u5728\u4e8e\uff0c\u5b83\u4eec\u662f\u4e0d\u53ef\u53d8\u7684\uff0c\u63d0\u4f9b\u4e86\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u65b9\u5f0f\u6765\u7ba1\u7406\u4e00\u7ec4\u76f8\u5173\u7684\u5c5e\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u6570\u636e\u7c7b<\/h3>\n<\/p>\n<p><p>Python 3.7\u5f15\u5165\u4e86\u6570\u636e\u7c7b\uff08dataclasses\uff09\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u6d01\u7684\u65b9\u5f0f\u6765\u5b9a\u4e49\u7c7b\uff0c\u5e76\u81ea\u52a8\u751f\u6210\u5e38\u89c1\u7684\u7279\u6b8a\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from dataclasses import dataclass<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a\u6570\u636e\u7c7b<\/strong><\/h2>\n<p>@dataclass<\/p>\n<p>class Person:<\/p>\n<p>    name: str<\/p>\n<p>    age: int<\/p>\n<p>    occupation: str<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u6570\u636e\u7c7b\u5b9e\u4f8b<\/strong><\/h2>\n<p>person = Person(name=&quot;John Doe&quot;, age=30, occupation=&quot;Engineer&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6570\u636e\u7c7b\u4f7f\u5f97\u7c7b\u7684\u5b9a\u4e49\u66f4\u52a0\u7b80\u6d01\uff0c\u5e76\u4e14\u81ea\u52a8\u751f\u6210\u5982<code>__init__<\/code>\u3001<code>__repr__<\/code>\u7b49\u65b9\u6cd5\uff0c\u9002\u5408\u7528\u4e8e\u8868\u793a\u7b80\u5355\u7684\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528\u679a\u4e3e\u7c7b<\/h3>\n<\/p>\n<p><p>\u5f53\u9700\u8981\u4e3a\u4e00\u7ec4\u5e38\u91cf\u6dfb\u52a0\u540d\u79f0\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u679a\u4e3e\u7c7b\u3002\u679a\u4e3e\u7c7b\u80fd\u591f\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from enum import Enum<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a\u679a\u4e3e\u7c7b<\/strong><\/h2>\n<p>class Status(Enum):<\/p>\n<p>    NEW = &quot;new&quot;<\/p>\n<p>    IN_PROGRESS = &quot;in_progress&quot;<\/p>\n<p>    COMPLETED = &quot;completed&quot;<\/p>\n<h2><strong>\u4f7f\u7528\u679a\u4e3e\u7c7b<\/strong><\/h2>\n<p>current_status = Status.NEW<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u679a\u4e3e\u7c7b\u63d0\u4f9b\u4e86\u4e00\u79cd\u6e05\u6670\u7684\u65b9\u5f0f\u6765\u5b9a\u4e49\u4e00\u7ec4\u76f8\u5173\u5e38\u91cf\uff0c\u5e76\u786e\u4fdd\u8fd9\u4e9b\u5e38\u91cf\u7684\u552f\u4e00\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u7ed3\u8bba<\/h3>\n<\/p>\n<p><p>\u4e3a\u6570\u636e\u6216\u5bf9\u8c61\u6dfb\u52a0\u540d\u79f0\u662f\u7f16\u7a0b\u4e2d\u7684\u5e38\u89c1\u64cd\u4f5c\uff0cPython\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u5f0f\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u9700\u6c42\u548c\u6570\u636e\u7ed3\u6784\u7684\u590d\u6742\u6027\u3002\u4f7f\u7528\u5408\u9002\u7684\u547d\u540d\u65b9\u5f0f\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\uff0c\u662f\u7f16\u5199\u9ad8\u8d28\u91cf\u4ee3\u7801\u7684\u91cd\u8981\u4e00\u6b65\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u7ed9\u53d8\u91cf\u547d\u540d\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53d8\u91cf\u547d\u540d\u53ef\u4ee5\u4f7f\u7528\u5b57\u6bcd\u3001\u6570\u5b57\u548c\u4e0b\u5212\u7ebf\uff0c\u4f46\u4e0d\u80fd\u4ee5\u6570\u5b57\u5f00\u5934\u3002\u901a\u5e38\u63a8\u8350\u4f7f\u7528\u6709\u610f\u4e49\u7684\u540d\u79f0\uff0c\u4ee5\u4fbf\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u66f4\u9ad8\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>user_name<\/code>\u6216<code>account_balance<\/code>\u6765\u63cf\u8ff0\u53d8\u91cf\u7684\u7528\u9014\u3002\u540c\u65f6\uff0c\u9075\u5faaPEP 8\u7f16\u7801\u89c4\u8303\uff0c\u4f7f\u7528\u5c0f\u5199\u5b57\u6bcd\u548c\u4e0b\u5212\u7ebf\u5206\u9694\u5355\u8bcd\uff0c\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u6e05\u6670\u5ea6\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u7ed9\u51fd\u6570\u547d\u540d\uff1f<\/strong><br \/>\u51fd\u6570\u547d\u540d\u9075\u5faa\u4e0e\u53d8\u91cf\u76f8\u4f3c\u7684\u89c4\u5219\u3002\u51fd\u6570\u540d\u79f0\u5e94\u7b80\u6d01\u4e14\u5177\u6709\u63cf\u8ff0\u6027\uff0c\u901a\u5e38\u4f7f\u7528\u5c0f\u5199\u5b57\u6bcd\u548c\u4e0b\u5212\u7ebf\u5206\u9694\u3002\u4f8b\u5982\uff0c<code>calculate_total()<\/code>\u6216<code>fetch_user_data()<\/code>\u662f\u4e0d\u9519\u7684\u547d\u540d\u9009\u62e9\u3002\u786e\u4fdd\u51fd\u6570\u540d\u79f0\u80fd\u591f\u6e05\u695a\u8868\u8fbe\u5176\u529f\u80fd\uff0c\u65b9\u4fbf\u5176\u4ed6\u5f00\u53d1\u8005\u7406\u89e3\u4ee3\u7801\u7684\u610f\u56fe\u3002<\/p>\n<p><strong>Python\u4e2d\u662f\u5426\u6709\u547d\u540d\u9650\u5236\u6216\u6700\u4f73\u5b9e\u8df5\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u907f\u514d\u4f7f\u7528\u4fdd\u7559\u5b57\uff08\u5982<code>if<\/code>\u3001<code>for<\/code>\u3001<code>while<\/code>\u7b49\uff09\u4f5c\u4e3a\u53d8\u91cf\u6216\u51fd\u6570\u540d\u79f0\u3002\u6b64\u5916\uff0c\u5c3d\u91cf\u907f\u514d\u4f7f\u7528\u5355\u5b57\u7b26\u53d8\u91cf\u540d\uff0c\u9664\u975e\u5728\u5faa\u73af\u6216\u4e34\u65f6\u8ba1\u7b97\u4e2d\u4f7f\u7528\u3002\u4e3a\u63d0\u9ad8\u4ee3\u7801\u53ef\u8bfb\u6027\uff0c\u8003\u8651\u4f7f\u7528\u6709\u610f\u4e49\u7684\u540d\u79f0\u548c\u9075\u5faa\u547d\u540d\u7ea6\u5b9a\uff0c\u4f8b\u5982\u7c7b\u540d\u4f7f\u7528\u9a7c\u5cf0\u547d\u540d\u6cd5\uff08\u5982<code>MyClass<\/code>\uff09\uff0c\u800c\u666e\u901a\u51fd\u6570\u548c\u53d8\u91cf\u4f7f\u7528\u5c0f\u5199\u5b57\u6bcd\u52a0\u4e0b\u5212\u7ebf\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u4e3a\u53d8\u91cf\u6216\u5bf9\u8c61\u6dfb\u52a0\u540d\u5b57\u7684\u65b9\u5f0f\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u53d8\u91cf\u547d\u540d\u3001\u521b\u5efa\u7c7b\u548c\u5bf9\u8c61\u5c5e\u6027\u3001\u4f7f\u7528\u5b57\u5178\u5b58\u50a8\u540d\u79f0\u4e0e\u503c\u7684\u5bf9 [&hellip;]","protected":false},"author":3,"featured_media":944583,"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\/944581"}],"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=944581"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/944581\/revisions"}],"predecessor-version":[{"id":944585,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/944581\/revisions\/944585"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/944583"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=944581"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=944581"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=944581"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}