{"id":1157437,"date":"2025-01-13T18:28:38","date_gmt":"2025-01-13T10:28:38","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1157437.html"},"modified":"2025-01-13T18:28:41","modified_gmt":"2025-01-13T10:28:41","slug":"python%e5%a6%82%e4%bd%95%e9%a2%84%e5%ae%9a%e4%b9%89%e5%8f%98%e9%87%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1157437.html","title":{"rendered":"python\u5982\u4f55\u9884\u5b9a\u4e49\u53d8\u91cf"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25195626\/d4a94cf2-f454-4b39-9c3e-47f27331626c.webp\" alt=\"python\u5982\u4f55\u9884\u5b9a\u4e49\u53d8\u91cf\" \/><\/p>\n<p><p> Python\u4e2d\u9884\u5b9a\u4e49\u53d8\u91cf\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff1a<strong>\u8d4b\u503c\u8bed\u53e5\u3001\u51fd\u6570\u53c2\u6570\u3001\u7c7b\u5c5e\u6027\u3001\u5168\u5c40\u53d8\u91cf<\/strong>\u3002\u5176\u4e2d\uff0c\u8d4b\u503c\u8bed\u53e5\u662f\u6700\u5e38\u7528\u7684\u9884\u5b9a\u4e49\u53d8\u91cf\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u7b80\u5355\u76f4\u63a5\u3002\u901a\u8fc7\u8d4b\u503c\u8bed\u53e5\uff0c\u4f60\u53ef\u4ee5\u5728\u4efb\u4f55\u5730\u65b9\u5b9a\u4e49\u53d8\u91cf\uff0c\u5e76\u7ed9\u5b83\u8d4b\u521d\u59cb\u503c\u3002\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u51e0\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u8d4b\u503c\u8bed\u53e5<\/h3>\n<\/p>\n<p><p>\u8d4b\u503c\u8bed\u53e5\u662f\u6700\u5e38\u89c1\u7684\u53d8\u91cf\u9884\u5b9a\u4e49\u65b9\u5f0f\u3002\u5728Python\u4e2d\uff0c\u53d8\u91cf\u662f\u5728\u7b2c\u4e00\u6b21\u8d4b\u503c\u65f6\u88ab\u521b\u5efa\u7684\u3002\u4f60\u53ea\u9700\u8981\u9009\u62e9\u4e00\u4e2a\u53d8\u91cf\u540d\uff0c\u7136\u540e\u4f7f\u7528\u7b49\u53f7\u8d4b\u503c\u5373\u53ef\u3002\u53d8\u91cf\u540d\u53ef\u4ee5\u662f\u4efb\u4f55\u5408\u6cd5\u7684\u6807\u8bc6\u7b26\uff0c\u7531\u5b57\u6bcd\u3001\u6570\u5b57\u548c\u4e0b\u5212\u7ebf\u7ec4\u6210\uff0c\u4e14\u4e0d\u80fd\u4ee5\u6570\u5b57\u5f00\u5934\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u9884\u5b9a\u4e49\u4e00\u4e2a\u6574\u6570\u53d8\u91cf<\/p>\n<p>age = 25<\/p>\n<h2><strong>\u9884\u5b9a\u4e49\u4e00\u4e2a\u6d6e\u70b9\u6570\u53d8\u91cf<\/strong><\/h2>\n<p>height = 5.9<\/p>\n<h2><strong>\u9884\u5b9a\u4e49\u4e00\u4e2a\u5b57\u7b26\u4e32\u53d8\u91cf<\/strong><\/h2>\n<p>name = &quot;John Doe&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4f8b\u5b50\u4e2d\uff0c<code>age<\/code>, <code>height<\/code>, \u548c <code>name<\/code> \u88ab\u5206\u522b\u8d4b\u503c\u4e3a25, 5.9, \u548c &quot;John Doe&quot;\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u51fd\u6570\u53c2\u6570<\/h3>\n<\/p>\n<p><p>\u5728\u5b9a\u4e49\u51fd\u6570\u65f6\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u51fd\u6570\u53c2\u6570\u6765\u9884\u5b9a\u4e49\u53d8\u91cf\u3002\u51fd\u6570\u53c2\u6570\u53ef\u4ee5\u63a5\u53d7\u8c03\u7528\u51fd\u6570\u65f6\u4f20\u5165\u7684\u503c\uff0c\u540c\u65f6\u4e5f\u53ef\u4ee5\u6709\u9ed8\u8ba4\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def greet(name=&quot;Guest&quot;):<\/p>\n<p>    print(f&quot;Hello, {name}!&quot;)<\/p>\n<h2><strong>\u8c03\u7528\u51fd\u6570\u65f6\u5982\u679c\u4e0d\u4f20\u5165\u53c2\u6570\uff0c\u5c06\u4f7f\u7528\u9ed8\u8ba4\u503c<\/strong><\/h2>\n<p>greet()  # \u8f93\u51fa: Hello, Guest!<\/p>\n<h2><strong>\u8c03\u7528\u51fd\u6570\u65f6\u4f20\u5165\u53c2\u6570\uff0c\u5c06\u4f7f\u7528\u4f20\u5165\u7684\u503c<\/strong><\/h2>\n<p>greet(&quot;Alice&quot;)  # \u8f93\u51fa: Hello, Alice!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u51fd\u6570 <code>greet<\/code> \u9884\u5b9a\u4e49\u4e86\u4e00\u4e2a\u53c2\u6570 <code>name<\/code>\uff0c\u5176\u9ed8\u8ba4\u503c\u4e3a &quot;Guest&quot;\u3002\u5f53\u8c03\u7528\u51fd\u6570\u65f6\uff0c\u5982\u679c\u4e0d\u4f20\u5165\u53c2\u6570\uff0c<code>name<\/code> \u5c06\u4f7f\u7528\u9ed8\u8ba4\u503c\uff1b\u5982\u679c\u4f20\u5165\u53c2\u6570\uff0c\u5c06\u4f7f\u7528\u4f20\u5165\u7684\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u7c7b\u5c5e\u6027<\/h3>\n<\/p>\n<p><p>\u5728\u5b9a\u4e49\u7c7b\u65f6\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u7c7b\u5c5e\u6027\u6765\u9884\u5b9a\u4e49\u53d8\u91cf\u3002\u7c7b\u5c5e\u6027\u53ef\u4ee5\u662f\u7c7b\u7ea7\u522b\u7684\uff0c\u4e5f\u53ef\u4ee5\u662f\u5b9e\u4f8b\u7ea7\u522b\u7684\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Person:<\/p>\n<p>    # \u7c7b\u7ea7\u522b\u5c5e\u6027<\/p>\n<p>    species = &quot;Homo sapiens&quot;<\/p>\n<p>    def __init__(self, name, age):<\/p>\n<p>        # \u5b9e\u4f8b\u7ea7\u522b\u5c5e\u6027<\/p>\n<p>        self.name = name<\/p>\n<p>        self.age = age<\/p>\n<h2><strong>\u8bbf\u95ee\u7c7b\u7ea7\u522b\u5c5e\u6027<\/strong><\/h2>\n<p>print(Person.species)  # \u8f93\u51fa: Homo sapiens<\/p>\n<h2><strong>\u521b\u5efa\u5b9e\u4f8b\u5e76\u8bbf\u95ee\u5b9e\u4f8b\u7ea7\u522b\u5c5e\u6027<\/strong><\/h2>\n<p>person1 = Person(&quot;Alice&quot;, 30)<\/p>\n<p>print(person1.name)  # \u8f93\u51fa: Alice<\/p>\n<p>print(person1.age)  # \u8f93\u51fa: 30<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7c7b <code>Person<\/code> \u9884\u5b9a\u4e49\u4e86\u4e00\u4e2a\u7c7b\u7ea7\u522b\u5c5e\u6027 <code>species<\/code>\uff0c\u5176\u503c\u4e3a &quot;Homo sapiens&quot;\u3002\u540c\u65f6\uff0c<code>__init__<\/code> \u65b9\u6cd5\u5b9a\u4e49\u4e86\u4e24\u4e2a\u5b9e\u4f8b\u7ea7\u522b\u5c5e\u6027 <code>name<\/code> \u548c <code>age<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5168\u5c40\u53d8\u91cf<\/h3>\n<\/p>\n<p><p>\u5168\u5c40\u53d8\u91cf\u662f\u5728\u6574\u4e2a\u6a21\u5757\u8303\u56f4\u5185\u53ef\u8bbf\u95ee\u7684\u53d8\u91cf\u3002\u4f60\u53ef\u4ee5\u5728\u6a21\u5757\u7684\u9876\u5c42\u5b9a\u4e49\u5168\u5c40\u53d8\u91cf\uff0c\u7136\u540e\u5728\u51fd\u6570\u6216\u7c7b\u4e2d\u8bbf\u95ee\u5b83\u4eec\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u9884\u5b9a\u4e49\u5168\u5c40\u53d8\u91cf<\/p>\n<p>global_var = &quot;This is a global variable&quot;<\/p>\n<p>def print_global_var():<\/p>\n<p>    # \u8bbf\u95ee\u5168\u5c40\u53d8\u91cf<\/p>\n<p>    print(global_var)<\/p>\n<p>print_global_var()  # \u8f93\u51fa: This is a global variable<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>global_var<\/code> \u662f\u4e00\u4e2a\u5168\u5c40\u53d8\u91cf\uff0c\u53ef\u4ee5\u5728 <code>print_global_var<\/code> \u51fd\u6570\u4e2d\u76f4\u63a5\u8bbf\u95ee\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528<code>global<\/code> \u548c <code>nonlocal<\/code>\u5173\u952e\u5b57<\/h3>\n<\/p>\n<p><p>\u5728\u51fd\u6570\u4e2d\uff0c\u5982\u679c\u4f60\u60f3\u4fee\u6539\u5168\u5c40\u53d8\u91cf\u7684\u503c\uff0c\u6216\u8005\u5728\u5d4c\u5957\u51fd\u6570\u4e2d\u4fee\u6539\u5916\u5c42\u51fd\u6570\u53d8\u91cf\u7684\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>global<\/code> \u548c <code>nonlocal<\/code>\u5173\u952e\u5b57\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u9884\u5b9a\u4e49\u5168\u5c40\u53d8\u91cf<\/p>\n<p>x = 10<\/p>\n<p>def modify_global():<\/p>\n<p>    global x<\/p>\n<p>    x = 20<\/p>\n<p>modify_global()<\/p>\n<p>print(x)  # \u8f93\u51fa: 20<\/p>\n<p>def outer():<\/p>\n<p>    y = 10<\/p>\n<p>    def inner():<\/p>\n<p>        nonlocal y<\/p>\n<p>        y = 20<\/p>\n<p>    inner()<\/p>\n<p>    print(y)<\/p>\n<p>outer()  # \u8f93\u51fa: 20<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>modify_global<\/code> \u51fd\u6570\u4f7f\u7528 <code>global<\/code> \u5173\u952e\u5b57\u6765\u4fee\u6539\u5168\u5c40\u53d8\u91cf <code>x<\/code> \u7684\u503c\uff0c\u800c <code>outer<\/code> \u51fd\u6570\u4e2d\u7684 <code>inner<\/code> \u51fd\u6570\u4f7f\u7528 <code>nonlocal<\/code> \u5173\u952e\u5b57\u6765\u4fee\u6539\u5916\u5c42\u51fd\u6570\u53d8\u91cf <code>y<\/code> \u7684\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u6a21\u5757\u7ea7\u53d8\u91cf<\/h3>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u5728\u4e00\u4e2a\u6a21\u5757\u4e2d\u5b9a\u4e49\u53d8\u91cf\uff0c\u7136\u540e\u5728\u5176\u4ed6\u6a21\u5757\u4e2d\u5bfc\u5165\u5e76\u4f7f\u7528\u5b83\u4eec\u3002\u8fd9\u662f\u4e00\u79cd\u5728\u591a\u4e2a\u6a21\u5757\u4e4b\u95f4\u5171\u4eab\u53d8\u91cf\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5728 module_a.py \u4e2d\u9884\u5b9a\u4e49\u53d8\u91cf<\/p>\n<p>shared_var = &quot;This is a shared variable&quot;<\/p>\n<h2><strong>\u5728 module_b.py \u4e2d\u4f7f\u7528\u53d8\u91cf<\/strong><\/h2>\n<p>from module_a import shared_var<\/p>\n<p>print(shared_var)  # \u8f93\u51fa: This is a shared variable<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u4f60\u53ef\u4ee5\u5728\u4e00\u4e2a\u6a21\u5757\u4e2d\u9884\u5b9a\u4e49\u53d8\u91cf\uff0c\u7136\u540e\u5728\u5176\u4ed6\u6a21\u5757\u4e2d\u4f7f\u7528\u5b83\u4eec\uff0c\u4ece\u800c\u5b9e\u73b0\u6a21\u5757\u4e4b\u95f4\u7684\u6570\u636e\u5171\u4eab\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528\u73af\u5883\u53d8\u91cf<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u5728\u7a0b\u5e8f\u8fd0\u884c\u65f6\u4f7f\u7528\u73af\u5883\u53d8\u91cf\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>os<\/code> \u6a21\u5757\u6765\u8bfb\u53d6\u548c\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf<\/strong><\/h2>\n<p>os.environ[&quot;MY_VAR&quot;] = &quot;This is an environment variable&quot;<\/p>\n<h2><strong>\u8bfb\u53d6\u73af\u5883\u53d8\u91cf<\/strong><\/h2>\n<p>my_var = os.getenv(&quot;MY_VAR&quot;)<\/p>\n<p>print(my_var)  # \u8f93\u51fa: This is an environment variable<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>os.environ<\/code> \u7528\u4e8e\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf <code>MY_VAR<\/code>\uff0c<code>os.getenv<\/code> \u7528\u4e8e\u8bfb\u53d6\u73af\u5883\u53d8\u91cf <code>MY_VAR<\/code> \u7684\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u914d\u7f6e\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5728\u5927\u578b\u9879\u76ee\u4e2d\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u4f7f\u7528\u914d\u7f6e\u6587\u4ef6\u6765\u9884\u5b9a\u4e49\u53d8\u91cf\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>configparser<\/code> \u6a21\u5757\u6765\u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import configparser<\/p>\n<h2><strong>\u521b\u5efa\u914d\u7f6e\u6587\u4ef6\u5bf9\u8c61<\/strong><\/h2>\n<p>config = configparser.ConfigParser()<\/p>\n<h2><strong>\u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6<\/strong><\/h2>\n<p>config.read(&#39;config.ini&#39;)<\/p>\n<h2><strong>\u83b7\u53d6\u914d\u7f6e\u53d8\u91cf<\/strong><\/h2>\n<p>db_host = config[&#39;database&#39;][&#39;host&#39;]<\/p>\n<p>db_port = config[&#39;database&#39;][&#39;port&#39;]<\/p>\n<p>print(db_host)  # \u8f93\u51fa: localhost<\/p>\n<p>print(db_port)  # \u8f93\u51fa: 5432<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>configparser<\/code> \u6a21\u5757\u7528\u4e8e\u8bfb\u53d6 <code>config.ini<\/code> \u914d\u7f6e\u6587\u4ef6\uff0c\u5e76\u83b7\u53d6\u914d\u7f6e\u6587\u4ef6\u4e2d\u7684\u53d8\u91cf <code>host<\/code> \u548c <code>port<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u5e38\u91cf<\/h3>\n<\/p>\n<p><p>\u5728\u4e00\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u9884\u5b9a\u4e49\u4e00\u4e9b\u5e38\u91cf\u3002\u867d\u7136Python\u6ca1\u6709\u5185\u7f6e\u7684\u5e38\u91cf\u7c7b\u578b\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u5168\u5927\u5199\u7684\u53d8\u91cf\u540d\u6765\u8868\u793a\u5e38\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u9884\u5b9a\u4e49\u5e38\u91cf<\/p>\n<p>PI = 3.14159<\/p>\n<p>GRAVITY = 9.8<\/p>\n<p>print(PI)  # \u8f93\u51fa: 3.14159<\/p>\n<p>print(GRAVITY)  # \u8f93\u51fa: 9.8<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>PI<\/code> \u548c <code>GRAVITY<\/code> \u662f\u4e24\u4e2a\u5e38\u91cf\uff0c\u4f7f\u7528\u5168\u5927\u5199\u5b57\u6bcd\u8868\u793a\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u7c7b\u578b\u6ce8\u89e3<\/h3>\n<\/p>\n<p><p>\u7c7b\u578b\u6ce8\u89e3\u53ef\u4ee5\u5e2e\u52a9\u4f60\u9884\u5b9a\u4e49\u53d8\u91cf\u7684\u7c7b\u578b\uff0c\u4ece\u800c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u9884\u5b9a\u4e49\u53d8\u91cf\u5e76\u6dfb\u52a0\u7c7b\u578b\u6ce8\u89e3<\/p>\n<p>age: int = 25<\/p>\n<p>height: float = 5.9<\/p>\n<p>name: str = &quot;John Doe&quot;<\/p>\n<p>print(age)  # \u8f93\u51fa: 25<\/p>\n<p>print(height)  # \u8f93\u51fa: 5.9<\/p>\n<p>print(name)  # \u8f93\u51fa: John Doe<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u53d8\u91cf <code>age<\/code>\u3001<code>height<\/code> \u548c <code>name<\/code> \u88ab\u5206\u522b\u6ce8\u89e3\u4e3a <code>int<\/code>\u3001<code>float<\/code> \u548c <code>str<\/code> \u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e00\u3001\u4f7f\u7528\u5b57\u5178<\/h3>\n<\/p>\n<p><p>\u5b57\u5178\u662f\u4e00\u79cd\u975e\u5e38\u7075\u6d3b\u7684\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u7528\u6765\u9884\u5b9a\u4e49\u4e00\u7ec4\u76f8\u5173\u7684\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u9884\u5b9a\u4e49\u5b57\u5178<\/p>\n<p>person = {<\/p>\n<p>    &quot;name&quot;: &quot;John Doe&quot;,<\/p>\n<p>    &quot;age&quot;: 25,<\/p>\n<p>    &quot;height&quot;: 5.9<\/p>\n<p>}<\/p>\n<p>print(person[&quot;name&quot;])  # \u8f93\u51fa: John Doe<\/p>\n<p>print(person[&quot;age&quot;])  # \u8f93\u51fa: 25<\/p>\n<p>print(person[&quot;height&quot;])  # \u8f93\u51fa: 5.9<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5b57\u5178 <code>person<\/code> \u88ab\u7528\u6765\u5b58\u50a8 <code>name<\/code>\u3001<code>age<\/code> \u548c <code>height<\/code> \u8fd9\u4e09\u4e2a\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e8c\u3001\u4f7f\u7528\u96c6\u5408<\/h3>\n<\/p>\n<p><p>\u96c6\u5408\u662f\u4e00\u79cd\u65e0\u5e8f\u7684\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u7528\u6765\u5b58\u50a8\u4e00\u7ec4\u552f\u4e00\u7684\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u9884\u5b9a\u4e49\u96c6\u5408<\/p>\n<p>fruits = {&quot;apple&quot;, &quot;banana&quot;, &quot;cherry&quot;}<\/p>\n<p>print(fruits)  # \u8f93\u51fa: {&#39;banana&#39;, &#39;cherry&#39;, &#39;apple&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u96c6\u5408 <code>fruits<\/code> \u88ab\u7528\u6765\u5b58\u50a8 &quot;apple&quot;\u3001&quot;banana&quot; \u548c &quot;cherry&quot; \u8fd9\u4e09\u4e2a\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e09\u3001\u4f7f\u7528\u5143\u7ec4<\/h3>\n<\/p>\n<p><p>\u5143\u7ec4\u662f\u4e00\u79cd\u4e0d\u53ef\u53d8\u7684\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u7528\u6765\u5b58\u50a8\u4e00\u7ec4\u76f8\u5173\u7684\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u9884\u5b9a\u4e49\u5143\u7ec4<\/p>\n<p>person = (&quot;John Doe&quot;, 25, 5.9)<\/p>\n<p>print(person[0])  # \u8f93\u51fa: John Doe<\/p>\n<p>print(person[1])  # \u8f93\u51fa: 25<\/p>\n<p>print(person[2])  # \u8f93\u51fa: 5.9<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5143\u7ec4 <code>person<\/code> \u88ab\u7528\u6765\u5b58\u50a8 <code>name<\/code>\u3001<code>age<\/code> \u548c <code>height<\/code> \u8fd9\u4e09\u4e2a\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u56db\u3001\u4f7f\u7528\u5217\u8868<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u662f\u4e00\u79cd\u6709\u5e8f\u7684\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u7528\u6765\u5b58\u50a8\u4e00\u7ec4\u76f8\u5173\u7684\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u9884\u5b9a\u4e49\u5217\u8868<\/p>\n<p>fruits = [&quot;apple&quot;, &quot;banana&quot;, &quot;cherry&quot;]<\/p>\n<p>print(fruits[0])  # \u8f93\u51fa: apple<\/p>\n<p>print(fruits[1])  # \u8f93\u51fa: banana<\/p>\n<p>print(fruits[2])  # \u8f93\u51fa: cherry<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5217\u8868 <code>fruits<\/code> \u88ab\u7528\u6765\u5b58\u50a8 &quot;apple&quot;\u3001&quot;banana&quot; \u548c &quot;cherry&quot; \u8fd9\u4e09\u4e2a\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e94\u3001\u4f7f\u7528\u679a\u4e3e<\/h3>\n<\/p>\n<p><p>\u679a\u4e3e\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u7528\u6765\u8868\u793a\u4e00\u7ec4\u76f8\u5173\u7684\u5e38\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from enum import Enum<\/p>\n<h2><strong>\u9884\u5b9a\u4e49\u679a\u4e3e<\/strong><\/h2>\n<p>class Color(Enum):<\/p>\n<p>    RED = 1<\/p>\n<p>    GREEN = 2<\/p>\n<p>    BLUE = 3<\/p>\n<p>print(Color.RED)  # \u8f93\u51fa: Color.RED<\/p>\n<p>print(Color.GREEN)  # \u8f93\u51fa: Color.GREEN<\/p>\n<p>print(Color.BLUE)  # \u8f93\u51fa: Color.BLUE<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u679a\u4e3e <code>Color<\/code> \u88ab\u7528\u6765\u8868\u793a <code>RED<\/code>\u3001<code>GREEN<\/code> \u548c <code>BLUE<\/code> \u8fd9\u4e09\u4e2a\u5e38\u91cf\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u516d\u3001\u4f7f\u7528\u547d\u540d\u5143\u7ec4<\/h3>\n<\/p>\n<p><p>\u547d\u540d\u5143\u7ec4\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u5143\u7ec4\uff0c\u53ef\u4ee5\u901a\u8fc7\u540d\u79f0\u6765\u8bbf\u95ee\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import namedtuple<\/p>\n<h2><strong>\u9884\u5b9a\u4e49\u547d\u540d\u5143\u7ec4<\/strong><\/h2>\n<p>Person = namedtuple(&quot;Person&quot;, &quot;name age height&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u547d\u540d\u5143\u7ec4\u5b9e\u4f8b<\/strong><\/h2>\n<p>person = Person(name=&quot;John Doe&quot;, age=25, height=5.9)<\/p>\n<p>print(person.name)  # \u8f93\u51fa: John Doe<\/p>\n<p>print(person.age)  # \u8f93\u51fa: 25<\/p>\n<p>print(person.height)  # \u8f93\u51fa: 5.9<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u547d\u540d\u5143\u7ec4 <code>Person<\/code> \u88ab\u7528\u6765\u5b58\u50a8 <code>name<\/code>\u3001<code>age<\/code> \u548c <code>height<\/code> \u8fd9\u4e09\u4e2a\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0a\u662fPython\u4e2d\u9884\u5b9a\u4e49\u53d8\u91cf\u7684\u4e3b\u8981\u65b9\u6cd5\uff0c\u5305\u62ec\u8d4b\u503c\u8bed\u53e5\u3001\u51fd\u6570\u53c2\u6570\u3001\u7c7b\u5c5e\u6027\u3001\u5168\u5c40\u53d8\u91cf\u3001\u4f7f\u7528<code>global<\/code>\u548c<code>nonlocal<\/code>\u5173\u952e\u5b57\u3001\u6a21\u5757\u7ea7\u53d8\u91cf\u3001\u4f7f\u7528\u73af\u5883\u53d8\u91cf\u3001\u914d\u7f6e\u6587\u4ef6\u3001\u5e38\u91cf\u3001\u7c7b\u578b\u6ce8\u89e3\u3001\u4f7f\u7528\u5b57\u5178\u3001\u96c6\u5408\u3001\u5143\u7ec4\u3001\u5217\u8868\u3001\u679a\u4e3e\u548c\u547d\u540d\u5143\u7ec4\u7b49\u3002\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u5728\u4e0d\u540c\u7684\u4e0a\u4e0b\u6587\u4e2d\u9884\u5b9a\u4e49\u548c\u4f7f\u7528\u53d8\u91cf\uff0c\u4ece\u800c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u9884\u5b9a\u4e49\u53d8\u91cf\u5728Python\u4e2d\u6709\u4ec0\u4e48\u7528\u5904\uff1f<\/strong><br \/>\u9884\u5b9a\u4e49\u53d8\u91cf\u5728Python\u4e2d\u80fd\u591f\u5e2e\u52a9\u5f00\u53d1\u8005\u7b80\u5316\u4ee3\u7801\u7684\u7ed3\u6784\uff0c\u63d0\u9ad8\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u901a\u8fc7\u5728\u4ee3\u7801\u5f00\u59cb\u65f6\u5b9a\u4e49\u5e38\u7528\u7684\u53d8\u91cf\uff0c\u7528\u6237\u53ef\u4ee5\u907f\u514d\u91cd\u590d\u7f16\u5199\u76f8\u540c\u7684\u4ee3\u7801\u7247\u6bb5\uff0c\u540c\u65f6\u4e5f\u80fd\u5728\u9700\u8981\u4fee\u6539\u67d0\u4e9b\u6570\u503c\u65f6\uff0c\u53ea\u9700\u5728\u4e00\u4e2a\u5730\u65b9\u8fdb\u884c\u66f4\u6539\uff0c\u63d0\u9ad8\u6548\u7387\u3002<\/p>\n<p><strong>\u6211\u5e94\u8be5\u5982\u4f55\u9009\u62e9\u9884\u5b9a\u4e49\u53d8\u91cf\u7684\u547d\u540d\uff1f<\/strong><br \/>\u5728\u9009\u62e9\u9884\u5b9a\u4e49\u53d8\u91cf\u7684\u547d\u540d\u65f6\uff0c\u5efa\u8bae\u4f7f\u7528\u5177\u6709\u63cf\u8ff0\u6027\u7684\u540d\u79f0\uff0c\u4ee5\u4fbf\u5176\u4ed6\u5f00\u53d1\u8005\u6216\u60a8\u81ea\u5df1\u5728\u672a\u6765\u9605\u8bfb\u4ee3\u7801\u65f6\u80fd\u591f\u5feb\u901f\u7406\u89e3\u5176\u7528\u9014\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>max_users<\/code>\u6765\u8868\u793a\u6700\u5927\u7528\u6237\u6570\uff0c\u6216\u4f7f\u7528<code>default_timeout<\/code>\u6765\u8868\u793a\u9ed8\u8ba4\u8d85\u65f6\u65f6\u95f4\u3002\u9075\u5faaPython\u7684\u547d\u540d\u89c4\u8303\uff0c\u5982\u4f7f\u7528\u5c0f\u5199\u5b57\u6bcd\u548c\u4e0b\u5212\u7ebf\u5206\u9694\u8bcd\uff0c\u53ef\u4ee5\u8fdb\u4e00\u6b65\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u8fdb\u884c\u9884\u5b9a\u4e49\u53d8\u91cf\u7684\u7c7b\u578b\u58f0\u660e\uff1f<\/strong><br \/>Python\u662f\u4e00\u79cd\u52a8\u6001\u7c7b\u578b\u8bed\u8a00\uff0c\u56e0\u6b64\u4e0d\u9700\u8981\u5728\u5b9a\u4e49\u53d8\u91cf\u65f6\u58f0\u660e\u5176\u7c7b\u578b\u3002\u7136\u800c\uff0c\u4f7f\u7528\u7c7b\u578b\u6ce8\u89e3\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u6e05\u6670\u5ea6\u548c\u53ef\u8bfb\u6027\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u8fd9\u6837\u5b9a\u4e49\u53d8\u91cf\uff1a<code>max_users: int = 100<\/code>\u3002\u8fd9\u79cd\u65b9\u5f0f\u867d\u7136\u4e0d\u662f\u5f3a\u5236\u6027\u7684\uff0c\u4f46\u80fd\u5e2e\u52a9\u5176\u4ed6\u5f00\u53d1\u8005\u7406\u89e3\u53d8\u91cf\u7684\u9884\u671f\u6570\u636e\u7c7b\u578b\uff0c\u4ece\u800c\u51cf\u5c11\u6f5c\u5728\u7684\u9519\u8bef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u9884\u5b9a\u4e49\u53d8\u91cf\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff1a\u8d4b\u503c\u8bed\u53e5\u3001\u51fd\u6570\u53c2\u6570\u3001\u7c7b\u5c5e\u6027\u3001\u5168\u5c40\u53d8\u91cf\u3002\u5176\u4e2d\uff0c\u8d4b\u503c\u8bed\u53e5\u662f\u6700\u5e38\u7528\u7684\u9884\u5b9a\u4e49\u53d8\u91cf [&hellip;]","protected":false},"author":3,"featured_media":1157443,"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\/1157437"}],"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=1157437"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1157437\/revisions"}],"predecessor-version":[{"id":1157445,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1157437\/revisions\/1157445"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1157443"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1157437"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1157437"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1157437"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}