{"id":1042907,"date":"2024-12-31T13:01:22","date_gmt":"2024-12-31T05:01:22","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1042907.html"},"modified":"2024-12-31T13:01:24","modified_gmt":"2024-12-31T05:01:24","slug":"python%e5%a6%82%e4%bd%95%e5%ae%9a%e4%b9%89%e4%b8%a4%e4%b8%aa%e6%96%b0%e5%8f%98%e9%87%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1042907.html","title":{"rendered":"python\u5982\u4f55\u5b9a\u4e49\u4e24\u4e2a\u65b0\u53d8\u91cf"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/c922106b-89bf-4d5e-97f3-5a26298e97d5.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u5b9a\u4e49\u4e24\u4e2a\u65b0\u53d8\u91cf\" \/><\/p>\n<p><p> <strong>\u5728 Python \u4e2d\u5b9a\u4e49\u4e24\u4e2a\u65b0\u53d8\u91cf\u7684\u5e38\u89c1\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u8d4b\u503c\u64cd\u4f5c\u7b26\u3001\u540c\u65f6\u8d4b\u503c\u548c\u89e3\u5305\u3002\u8fd9\u4e9b\u65b9\u6cd5\u8ba9\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u3001\u6613\u8bfb\u3002\u5728\u8fd9\u4e9b\u65b9\u6cd5\u4e2d\uff0c\u8d4b\u503c\u64cd\u4f5c\u7b26\u7528\u4e8e\u5c06\u503c\u5206\u914d\u7ed9\u53d8\u91cf\uff0c\u540c\u65f6\u8d4b\u503c\u5141\u8bb8\u5728\u4e00\u884c\u4e2d\u5b9a\u4e49\u591a\u4e2a\u53d8\u91cf\uff0c\u89e3\u5305\u5219\u7528\u4e8e\u4ece\u5e8f\u5217\u4e2d\u63d0\u53d6\u503c\u3002<\/strong> \u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u63a2\u8ba8\u5b83\u4eec\u7684\u9002\u7528\u573a\u666f\u548c\u4f18\u52bf\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u8d4b\u503c\u64cd\u4f5c\u7b26<\/h3>\n<\/p>\n<p><p>\u8d4b\u503c\u64cd\u4f5c\u7b26\u662f\u5b9a\u4e49\u53d8\u91cf\u7684\u6700\u57fa\u672c\u65b9\u6cd5\u3002\u5b83\u4f7f\u7528\u7b49\u53f7\uff08=\uff09\u5c06\u4e00\u4e2a\u503c\u8d4b\u7ed9\u4e00\u4e2a\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u4e24\u4e2a\u53d8\u91cf<\/p>\n<p>x = 10<\/p>\n<p>y = 20<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0cx \u88ab\u8d4b\u503c\u4e3a 10\uff0cy \u88ab\u8d4b\u503c\u4e3a 20\u3002\u8d4b\u503c\u64cd\u4f5c\u7b26\u7b80\u5355\u660e\u4e86\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u540c\u65f6\u8d4b\u503c<\/h3>\n<\/p>\n<p><p>Python \u652f\u6301\u540c\u65f6\u8d4b\u503c\uff0c\u53ef\u4ee5\u5728\u4e00\u884c\u4e2d\u5b9a\u4e49\u591a\u4e2a\u53d8\u91cf\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u7b80\u6d01\uff0c\u800c\u4e14\u51cf\u5c11\u4e86\u4ee3\u7801\u884c\u6570\uff0c\u63d0\u9ad8\u4e86\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u540c\u65f6\u5b9a\u4e49\u4e24\u4e2a\u53d8\u91cf<\/p>\n<p>a, b = 30, 40<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0ca \u88ab\u8d4b\u503c\u4e3a 30\uff0cb \u88ab\u8d4b\u503c\u4e3a 40\u3002\u901a\u8fc7\u540c\u65f6\u8d4b\u503c\uff0c\u53ef\u4ee5\u907f\u514d\u591a\u6b21\u4f7f\u7528\u8d4b\u503c\u64cd\u4f5c\u7b26\uff0c\u6709\u6548\u7b80\u5316\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u89e3\u5305<\/h3>\n<\/p>\n<p><p>\u89e3\u5305\u662f\u4e00\u79cd\u9ad8\u7ea7\u6280\u5de7\uff0c\u7528\u4e8e\u4ece\u5e8f\u5217\u4e2d\u63d0\u53d6\u591a\u4e2a\u503c\u5e76\u8d4b\u7ed9\u53d8\u91cf\u3002\u5e38\u89c1\u7684\u5e8f\u5217\u7c7b\u578b\u5305\u62ec\u5217\u8868\u3001\u5143\u7ec4\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u89e3\u5305\u5b9a\u4e49\u4e24\u4e2a\u53d8\u91cf<\/p>\n<p>numbers = (50, 60)<\/p>\n<p>c, d = numbers<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0cnumbers \u662f\u4e00\u4e2a\u5143\u7ec4\uff0c\u5305\u542b\u4e24\u4e2a\u503c\u3002\u901a\u8fc7\u89e3\u5305\uff0cc \u88ab\u8d4b\u503c\u4e3a 50\uff0cd \u88ab\u8d4b\u503c\u4e3a 60\u3002\u89e3\u5305\u7279\u522b\u9002\u7528\u4e8e\u51fd\u6570\u8fd4\u56de\u591a\u4e2a\u503c\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u5217\u8868\u548c\u5b57\u5178<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u65b9\u6cd5\uff0cPython \u8fd8\u53ef\u4ee5\u901a\u8fc7\u5217\u8868\u548c\u5b57\u5178\u6765\u5b9a\u4e49\u548c\u7ba1\u7406\u591a\u4e2a\u53d8\u91cf\u3002\u8fd9\u5728\u5904\u7406\u590d\u6742\u6570\u636e\u7ed3\u6784\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528\u5217\u8868<\/h4>\n<\/p>\n<p><p>\u5217\u8868\u662f\u4e00\u79cd\u6709\u5e8f\u96c6\u5408\uff0c\u53ef\u4ee5\u5b58\u50a8\u591a\u4e2a\u53d8\u91cf\uff0c\u5e76\u901a\u8fc7\u7d22\u5f15\u8bbf\u95ee\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u5217\u8868\u5b9a\u4e49\u53d8\u91cf<\/p>\n<p>variables = [70, 80]<\/p>\n<p>e, f = variables[0], variables[1]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0cvariables \u662f\u4e00\u4e2a\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u503c\u3002\u901a\u8fc7\u7d22\u5f15\u8bbf\u95ee\uff0ce \u88ab\u8d4b\u503c\u4e3a 70\uff0cf \u88ab\u8d4b\u503c\u4e3a 80\u3002\u5217\u8868\u9002\u7528\u4e8e\u9700\u8981\u52a8\u6001\u7ba1\u7406\u591a\u4e2a\u53d8\u91cf\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528\u5b57\u5178<\/h4>\n<\/p>\n<p><p>\u5b57\u5178\u662f\u4e00\u79cd\u65e0\u5e8f\u96c6\u5408\uff0c\u901a\u8fc7\u952e\u503c\u5bf9\u5b58\u50a8\u53d8\u91cf\u3002\u5b83\u5177\u6709\u9ad8\u6548\u7684\u67e5\u627e\u6027\u80fd\uff0c\u9002\u7528\u4e8e\u9700\u8981\u6839\u636e\u952e\u5feb\u901f\u8bbf\u95ee\u53d8\u91cf\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u5b57\u5178\u5b9a\u4e49\u53d8\u91cf<\/p>\n<p>variables_dict = {&#39;g&#39;: 90, &#39;h&#39;: 100}<\/p>\n<p>g = variables_dict[&#39;g&#39;]<\/p>\n<p>h = variables_dict[&#39;h&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0cvariables_dict \u662f\u4e00\u4e2a\u5b57\u5178\uff0c\u5305\u542b\u4e24\u4e2a\u952e\u503c\u5bf9\u3002\u901a\u8fc7\u952e\u8bbf\u95ee\uff0cg \u88ab\u8d4b\u503c\u4e3a 90\uff0ch \u88ab\u8d4b\u503c\u4e3a 100\u3002\u5b57\u5178\u9002\u7528\u4e8e\u9700\u8981\u6839\u636e\u952e\u52a8\u6001\u8bbf\u95ee\u548c\u7ba1\u7406\u53d8\u91cf\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u7c7b\u548c\u5bf9\u8c61<\/h3>\n<\/p>\n<p><p>\u5728\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u4e2d\uff0c\u7c7b\u548c\u5bf9\u8c61\u662f\u7ec4\u7ec7\u548c\u7ba1\u7406\u53d8\u91cf\u7684\u5f3a\u5927\u5de5\u5177\u3002\u901a\u8fc7\u5b9a\u4e49\u7c7b\uff0c\u53ef\u4ee5\u5c06\u76f8\u5173\u53d8\u91cf\u548c\u51fd\u6570\u5c01\u88c5\u5728\u4e00\u8d77\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u590d\u7528\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u7c7b\u5b9a\u4e49\u53d8\u91cf<\/p>\n<p>class Variables:<\/p>\n<p>    def __init__(self, i, j):<\/p>\n<p>        self.i = i<\/p>\n<p>        self.j = j<\/p>\n<h2><strong>\u521b\u5efa\u5bf9\u8c61\u5e76\u5b9a\u4e49\u53d8\u91cf<\/strong><\/h2>\n<p>variables_obj = Variables(110, 120)<\/p>\n<p>i = variables_obj.i<\/p>\n<p>j = variables_obj.j<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0cVariables \u662f\u4e00\u4e2a\u7c7b\uff0c\u5305\u542b\u4e24\u4e2a\u53d8\u91cf i \u548c j\u3002\u901a\u8fc7\u521b\u5efa\u5bf9\u8c61 variables_obj\uff0c\u5e76\u8bbf\u95ee\u5176\u5c5e\u6027\uff0ci \u88ab\u8d4b\u503c\u4e3a 110\uff0cj \u88ab\u8d4b\u503c\u4e3a 120\u3002\u7c7b\u548c\u5bf9\u8c61\u9002\u7528\u4e8e\u5927\u578b\u9879\u76ee\uff0c\u9700\u8981\u7ed3\u6784\u5316\u7ba1\u7406\u53d8\u91cf\u548c\u51fd\u6570\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528\u51fd\u6570\u548c\u8fd4\u56de\u503c<\/h3>\n<\/p>\n<p><p>\u51fd\u6570\u662f\u7ec4\u7ec7\u548c\u7ba1\u7406\u4ee3\u7801\u7684\u91cd\u8981\u5de5\u5177\u3002\u901a\u8fc7\u5b9a\u4e49\u548c\u8c03\u7528\u51fd\u6570\uff0c\u53ef\u4ee5\u5c06\u76f8\u5173\u53d8\u91cf\u548c\u903b\u8f91\u5c01\u88c5\u5728\u4e00\u8d77\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u590d\u7528\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u51fd\u6570\u8fd4\u56de\u4e24\u4e2a\u53d8\u91cf<\/p>\n<p>def get_variables():<\/p>\n<p>    return 130, 140<\/p>\n<h2><strong>\u8c03\u7528\u51fd\u6570\u5e76\u5b9a\u4e49\u53d8\u91cf<\/strong><\/h2>\n<p>k, l = get_variables()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0cget_variables \u662f\u4e00\u4e2a\u51fd\u6570\uff0c\u8fd4\u56de\u4e24\u4e2a\u503c 130 \u548c 140\u3002\u901a\u8fc7\u8c03\u7528\u51fd\u6570\u5e76\u89e3\u5305\u8fd4\u56de\u503c\uff0ck \u88ab\u8d4b\u503c\u4e3a 130\uff0cl \u88ab\u8d4b\u503c\u4e3a 140\u3002\u51fd\u6570\u9002\u7528\u4e8e\u9700\u8981\u5c01\u88c5\u548c\u590d\u7528\u903b\u8f91\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528\u5168\u5c40\u53d8\u91cf<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u5728\u4e0d\u540c\u51fd\u6570\u6216\u6a21\u5757\u4e2d\u5171\u4eab\u53d8\u91cf\u3002\u8fd9\u65f6\u53ef\u4ee5\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u3002\u5168\u5c40\u53d8\u91cf\u5728\u5b9a\u4e49\u540e\uff0c\u53ef\u4ee5\u5728\u6574\u4e2a\u7a0b\u5e8f\u4e2d\u8bbf\u95ee\u548c\u4fee\u6539\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u5168\u5c40\u53d8\u91cf<\/p>\n<p>global m, n<\/p>\n<p>m, n = 150, 160<\/p>\n<h2><strong>\u5728\u51fd\u6570\u4e2d\u8bbf\u95ee\u548c\u4fee\u6539\u5168\u5c40\u53d8\u91cf<\/strong><\/h2>\n<p>def modify_variables():<\/p>\n<p>    global m, n<\/p>\n<p>    m += 10<\/p>\n<p>    n += 20<\/p>\n<p>modify_variables()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0cm \u548c n \u662f\u5168\u5c40\u53d8\u91cf\uff0c\u5728\u51fd\u6570 modify_variables \u4e2d\u88ab\u8bbf\u95ee\u548c\u4fee\u6539\u3002\u5168\u5c40\u53d8\u91cf\u9002\u7528\u4e8e\u9700\u8981\u5728\u591a\u4e2a\u51fd\u6570\u6216\u6a21\u5757\u4e2d\u5171\u4eab\u6570\u636e\u7684\u60c5\u51b5\uff0c\u4f46\u5e94\u8c28\u614e\u4f7f\u7528\uff0c\u4ee5\u907f\u514d\u547d\u540d\u51b2\u7a81\u548c\u96be\u4ee5\u8c03\u8bd5\u7684\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u4f7f\u7528\u73af\u5883\u53d8\u91cf<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u5e94\u7528\u573a\u666f\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u4f7f\u7528\u73af\u5883\u53d8\u91cf\u6765\u5b9a\u4e49\u548c\u7ba1\u7406\u53d8\u91cf\u3002\u73af\u5883\u53d8\u91cf\u901a\u5e38\u7528\u4e8e\u914d\u7f6e\u548c\u7ba1\u7406\u5e94\u7528\u7a0b\u5e8f\u7684\u8fd0\u884c\u73af\u5883\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[&#39;VAR_O&#39;] = &#39;170&#39;<\/p>\n<p>os.environ[&#39;VAR_P&#39;] = &#39;180&#39;<\/p>\n<h2><strong>\u83b7\u53d6\u73af\u5883\u53d8\u91cf<\/strong><\/h2>\n<p>o = os.environ.get(&#39;VAR_O&#39;)<\/p>\n<p>p = os.environ.get(&#39;VAR_P&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7 os \u6a21\u5757\u8bbe\u7f6e\u548c\u83b7\u53d6\u73af\u5883\u53d8\u91cf VAR_O \u548c VAR_P\u3002\u73af\u5883\u53d8\u91cf\u9002\u7528\u4e8e\u9700\u8981\u5728\u4e0d\u540c\u73af\u5883\u4e2d\u914d\u7f6e\u548c\u7ba1\u7406\u5e94\u7528\u7a0b\u5e8f\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u4f7f\u7528\u914d\u7f6e\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5728\u5927\u578b\u9879\u76ee\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u4f7f\u7528\u914d\u7f6e\u6587\u4ef6\u6765\u5b9a\u4e49\u548c\u7ba1\u7406\u53d8\u91cf\u3002\u914d\u7f6e\u6587\u4ef6\u53ef\u4ee5\u662f JSON\u3001YAML\u3001INI \u7b49\u683c\u5f0f\uff0c\u901a\u8fc7\u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6\uff0c\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u52a8\u6001\u52a0\u8f7d\u548c\u7ba1\u7406\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<h2><strong>\u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;config.json&#39;, &#39;r&#39;) as file:<\/p>\n<p>    config = json.load(file)<\/p>\n<h2><strong>\u83b7\u53d6\u53d8\u91cf<\/strong><\/h2>\n<p>q = config[&#39;var_q&#39;]<\/p>\n<p>r = config[&#39;var_r&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7\u8bfb\u53d6 JSON \u683c\u5f0f\u7684\u914d\u7f6e\u6587\u4ef6 config.json\uff0c\u5e76\u83b7\u53d6\u53d8\u91cf var_q \u548c var_r\u3002\u914d\u7f6e\u6587\u4ef6\u9002\u7528\u4e8e\u9700\u8981\u52a8\u6001\u52a0\u8f7d\u548c\u7ba1\u7406\u5927\u91cf\u914d\u7f6e\u6570\u636e\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u4f7f\u7528\u547d\u4ee4\u884c\u53c2\u6570<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u5e94\u7528\u573a\u666f\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u901a\u8fc7\u547d\u4ee4\u884c\u53c2\u6570\u6765\u5b9a\u4e49\u548c\u7ba1\u7406\u53d8\u91cf\u3002\u901a\u8fc7\u89e3\u6790\u547d\u4ee4\u884c\u53c2\u6570\uff0c\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u52a8\u6001\u83b7\u53d6\u548c\u7ba1\u7406\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import argparse<\/p>\n<h2><strong>\u5b9a\u4e49\u547d\u4ee4\u884c\u53c2\u6570<\/strong><\/h2>\n<p>parser = argparse.ArgumentParser()<\/p>\n<p>parser.add_argument(&#39;--var_s&#39;, type=int, default=190)<\/p>\n<p>parser.add_argument(&#39;--var_t&#39;, type=int, default=200)<\/p>\n<h2><strong>\u89e3\u6790\u547d\u4ee4\u884c\u53c2\u6570<\/strong><\/h2>\n<p>args = parser.parse_args()<\/p>\n<p>s = args.var_s<\/p>\n<p>t = args.var_t<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7 argparse \u6a21\u5757\u5b9a\u4e49\u548c\u89e3\u6790\u547d\u4ee4\u884c\u53c2\u6570 var_s \u548c var_t\u3002\u547d\u4ee4\u884c\u53c2\u6570\u9002\u7528\u4e8e\u9700\u8981\u5728\u8fd0\u884c\u65f6\u52a8\u6001\u4f20\u9012\u548c\u7ba1\u7406\u53d8\u91cf\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e00\u3001\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668<\/h3>\n<\/p>\n<p><p>\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u662f\u4e00\u79cd\u9ad8\u7ea7\u5de5\u5177\uff0c\u7528\u4e8e\u7ba1\u7406\u8d44\u6e90\u548c\u4e0a\u4e0b\u6587\u3002\u901a\u8fc7\u5b9a\u4e49\u548c\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\uff0c\u53ef\u4ee5\u5728\u7279\u5b9a\u8303\u56f4\u5185\u5b9a\u4e49\u548c\u7ba1\u7406\u53d8\u91cf\uff0c\u5e76\u5728\u9000\u51fa\u8303\u56f4\u65f6\u81ea\u52a8\u6e05\u7406\u8d44\u6e90\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from contextlib import contextmanager<\/p>\n<h2><strong>\u5b9a\u4e49\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668<\/strong><\/h2>\n<p>@contextmanager<\/p>\n<p>def manage_variables():<\/p>\n<p>    u, v = 210, 220<\/p>\n<p>    yield u, v<\/p>\n<p>    # \u6e05\u7406\u8d44\u6e90<\/p>\n<p>    print(&#39;Exiting context&#39;)<\/p>\n<h2><strong>\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u5b9a\u4e49\u53d8\u91cf<\/strong><\/h2>\n<p>with manage_variables() as (u, v):<\/p>\n<p>    print(u, v)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7 contextlib \u6a21\u5757\u5b9a\u4e49\u548c\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668 manage_variables\uff0c\u5e76\u5728\u7279\u5b9a\u8303\u56f4\u5185\u5b9a\u4e49\u548c\u7ba1\u7406\u53d8\u91cf u \u548c v\u3002\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u9002\u7528\u4e8e\u9700\u8981\u5728\u7279\u5b9a\u8303\u56f4\u5185\u5b9a\u4e49\u548c\u7ba1\u7406\u53d8\u91cf\uff0c\u5e76\u5728\u9000\u51fa\u8303\u56f4\u65f6\u81ea\u52a8\u6e05\u7406\u8d44\u6e90\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e8c\u3001\u4f7f\u7528\u88c5\u9970\u5668<\/h3>\n<\/p>\n<p><p>\u88c5\u9970\u5668\u662f\u4e00\u79cd\u9ad8\u7ea7\u5de5\u5177\uff0c\u7528\u4e8e\u6269\u5c55\u548c\u589e\u5f3a\u51fd\u6570\u7684\u529f\u80fd\u3002\u901a\u8fc7\u5b9a\u4e49\u548c\u4f7f\u7528\u88c5\u9970\u5668\uff0c\u53ef\u4ee5\u5728\u51fd\u6570\u8c03\u7528\u524d\u540e\u5b9a\u4e49\u548c\u7ba1\u7406\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u88c5\u9970\u5668<\/p>\n<p>def manage_variables(func):<\/p>\n<p>    def wrapper(*args, kwargs):<\/p>\n<p>        w, x = 230, 240<\/p>\n<p>        result = func(w, x, *args, kwargs)<\/p>\n<p>        return result<\/p>\n<p>    return wrapper<\/p>\n<h2><strong>\u4f7f\u7528\u88c5\u9970\u5668\u5b9a\u4e49\u53d8\u91cf<\/strong><\/h2>\n<p>@manage_variables<\/p>\n<p>def print_variables(w, x):<\/p>\n<p>    print(w, x)<\/p>\n<p>print_variables()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7\u5b9a\u4e49\u548c\u4f7f\u7528\u88c5\u9970\u5668 manage_variables\uff0c\u5728\u51fd\u6570 print_variables \u8c03\u7528\u524d\u5b9a\u4e49\u548c\u7ba1\u7406\u53d8\u91cf w \u548c x\u3002\u88c5\u9970\u5668\u9002\u7528\u4e8e\u9700\u8981\u6269\u5c55\u548c\u589e\u5f3a\u51fd\u6570\u529f\u80fd\uff0c\u5e76\u5728\u51fd\u6570\u8c03\u7528\u524d\u540e\u5b9a\u4e49\u548c\u7ba1\u7406\u53d8\u91cf\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e09\u3001\u4f7f\u7528\u751f\u6210\u5668<\/h3>\n<\/p>\n<p><p>\u751f\u6210\u5668\u662f\u4e00\u79cd\u9ad8\u7ea7\u5de5\u5177\uff0c\u7528\u4e8e\u751f\u6210\u548c\u7ba1\u7406\u5e8f\u5217\u6570\u636e\u3002\u901a\u8fc7\u5b9a\u4e49\u548c\u4f7f\u7528\u751f\u6210\u5668\uff0c\u53ef\u4ee5\u52a8\u6001\u751f\u6210\u548c\u7ba1\u7406\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u751f\u6210\u5668<\/p>\n<p>def variable_generator():<\/p>\n<p>    yield 250<\/p>\n<p>    yield 260<\/p>\n<h2><strong>\u4f7f\u7528\u751f\u6210\u5668\u5b9a\u4e49\u53d8\u91cf<\/strong><\/h2>\n<p>gen = variable_generator()<\/p>\n<p>y = next(gen)<\/p>\n<p>z = next(gen)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7\u5b9a\u4e49\u548c\u4f7f\u7528\u751f\u6210\u5668 variable_generator \u52a8\u6001\u751f\u6210\u548c\u7ba1\u7406\u53d8\u91cf y \u548c z\u3002\u751f\u6210\u5668\u9002\u7528\u4e8e\u9700\u8981\u52a8\u6001\u751f\u6210\u548c\u7ba1\u7406\u5e8f\u5217\u6570\u636e\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u56db\u3001\u4f7f\u7528\u7c7b\u578b\u6ce8\u89e3<\/h3>\n<\/p>\n<p><p>\u7c7b\u578b\u6ce8\u89e3\u662f\u4e00\u79cd\u5de5\u5177\uff0c\u7528\u4e8e\u4e3a\u53d8\u91cf\u548c\u51fd\u6570\u6dfb\u52a0\u7c7b\u578b\u4fe1\u606f\u3002\u901a\u8fc7\u4f7f\u7528\u7c7b\u578b\u6ce8\u89e3\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\uff0c\u5e76\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u7c7b\u578b\u6ce8\u89e3\u5b9a\u4e49\u53d8\u91cf<\/p>\n<p>aa: int = 270<\/p>\n<p>bb: int = 280<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7\u7c7b\u578b\u6ce8\u89e3\u4e3a\u53d8\u91cf aa \u548c bb \u6dfb\u52a0\u7c7b\u578b\u4fe1\u606f\u3002\u7c7b\u578b\u6ce8\u89e3\u9002\u7528\u4e8e\u9700\u8981\u63d0\u9ad8\u4ee3\u7801\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e94\u3001\u4f7f\u7528\u6570\u636e\u7c7b<\/h3>\n<\/p>\n<p><p>\u6570\u636e\u7c7b\u662f\u4e00\u79cd\u5de5\u5177\uff0c\u7528\u4e8e\u5b9a\u4e49\u548c\u7ba1\u7406\u6570\u636e\u7ed3\u6784\u3002\u901a\u8fc7\u5b9a\u4e49\u548c\u4f7f\u7528\u6570\u636e\u7c7b\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\uff0c\u5e76\u7b80\u5316\u6570\u636e\u7ba1\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from dataclasses import dataclass<\/p>\n<h2><strong>\u5b9a\u4e49\u6570\u636e\u7c7b<\/strong><\/h2>\n<p>@dataclass<\/p>\n<p>class Variables:<\/p>\n<p>    cc: int<\/p>\n<p>    dd: int<\/p>\n<h2><strong>\u4f7f\u7528\u6570\u636e\u7c7b\u5b9a\u4e49\u53d8\u91cf<\/strong><\/h2>\n<p>variables = Variables(290, 300)<\/p>\n<p>cc = variables.cc<\/p>\n<p>dd = variables.dd<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7 dataclasses \u6a21\u5757\u5b9a\u4e49\u548c\u4f7f\u7528\u6570\u636e\u7c7b Variables\uff0c\u5e76\u7b80\u5316\u6570\u636e\u7ba1\u7406\u3002\u6570\u636e\u7c7b\u9002\u7528\u4e8e\u9700\u8981\u63d0\u9ad8\u4ee3\u7801\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\uff0c\u5e76\u7b80\u5316\u6570\u636e\u7ba1\u7406\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h3>\u7ed3\u8bba<\/h3>\n<\/p>\n<p><p>Python \u63d0\u4f9b\u4e86\u591a\u79cd\u5b9a\u4e49\u548c\u7ba1\u7406\u53d8\u91cf\u7684\u65b9\u6cd5\uff0c\u5305\u62ec\u8d4b\u503c\u64cd\u4f5c\u7b26\u3001\u540c\u65f6\u8d4b\u503c\u3001\u89e3\u5305\u3001\u5217\u8868\u3001\u5b57\u5178\u3001\u7c7b\u548c\u5bf9\u8c61\u3001\u51fd\u6570\u548c\u8fd4\u56de\u503c\u3001\u5168\u5c40\u53d8\u91cf\u3001\u73af\u5883\u53d8\u91cf\u3001\u914d\u7f6e\u6587\u4ef6\u3001\u547d\u4ee4\u884c\u53c2\u6570\u3001\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u3001\u88c5\u9970\u5668\u3001\u751f\u6210\u5668\u3001\u7c7b\u578b\u6ce8\u89e3\u548c\u6570\u636e\u7c7b\u7b49\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u573a\u666f\u548c\u4f18\u52bf\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u6765\u5b9a\u4e49\u548c\u7ba1\u7406\u53d8\u91cf\u3002\u901a\u8fc7\u5408\u7406\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3001\u53ef\u7ef4\u62a4\u6027\u548c\u6027\u80fd\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u540c\u65f6\u5b9a\u4e49\u4e24\u4e2a\u53d8\u91cf\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u9017\u53f7\u5206\u9694\u7684\u65b9\u5f0f\u6765\u540c\u65f6\u5b9a\u4e49\u591a\u4e2a\u53d8\u91cf\u3002\u4f8b\u5982\uff0c<code>a, b = 1, 2<\/code> \u8fd9\u884c\u4ee3\u7801\u4f1a\u5c061\u8d4b\u503c\u7ed9\u53d8\u91cfa\uff0c\u5c062\u8d4b\u503c\u7ed9\u53d8\u91cfb\u3002\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u7b80\u5316\u4ee3\u7801\uff0c\u4f7f\u5176\u66f4\u52a0\u7b80\u6d01\u548c\u6613\u8bfb\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5b9a\u4e49\u53d8\u91cf\u65f6\uff0c\u6709\u54ea\u4e9b\u5e38\u89c1\u7684\u547d\u540d\u89c4\u5219\uff1f<\/strong><br \/>\u53d8\u91cf\u547d\u540d\u65f6\u5e94\u9075\u5faa\u4e00\u5b9a\u7684\u89c4\u5219\u3002\u9996\u5148\uff0c\u53d8\u91cf\u540d\u5fc5\u987b\u4ee5\u5b57\u6bcd\u6216\u4e0b\u5212\u7ebf\u5f00\u5934\uff0c\u540e\u9762\u53ef\u4ee5\u8ddf\u5b57\u6bcd\u3001\u6570\u5b57\u6216\u4e0b\u5212\u7ebf\u3002\u53d8\u91cf\u540d\u4e0d\u80fd\u5305\u542b\u7a7a\u683c\u548c\u7279\u6b8a\u5b57\u7b26\uff0c\u5982@\u3001#\u3001$\u7b49\u3002\u6b64\u5916\uff0cPython\u662f\u533a\u5206\u5927\u5c0f\u5199\u7684\uff0c\u56e0\u6b64<code>myVar<\/code>\u548c<code>myvar<\/code>\u88ab\u89c6\u4e3a\u4e24\u4e2a\u4e0d\u540c\u7684\u53d8\u91cf\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u67e5\u770b\u5df2\u5b9a\u4e49\u7684\u53d8\u91cf\u53ca\u5176\u503c\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>print()<\/code>\u51fd\u6570\u6765\u67e5\u770b\u53d8\u91cf\u7684\u503c\u3002\u4f8b\u5982\uff0c\u5b9a\u4e49\u4e86\u53d8\u91cf\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7<code>print(a, b)<\/code>\u6765\u663e\u793a\u8fd9\u4e24\u4e2a\u53d8\u91cf\u7684\u503c\u3002\u6b64\u5916\uff0c\u4f7f\u7528<code>locals()<\/code>\u51fd\u6570\u53ef\u4ee5\u67e5\u770b\u5f53\u524d\u4f5c\u7528\u57df\u5185\u6240\u6709\u53d8\u91cf\u53ca\u5176\u5bf9\u5e94\u7684\u503c\uff0c\u9002\u5408\u8c03\u8bd5\u548c\u4e86\u89e3\u5f53\u524d\u73af\u5883\u7684\u53d8\u91cf\u72b6\u6001\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728 Python \u4e2d\u5b9a\u4e49\u4e24\u4e2a\u65b0\u53d8\u91cf\u7684\u5e38\u89c1\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u8d4b\u503c\u64cd\u4f5c\u7b26\u3001\u540c\u65f6\u8d4b\u503c\u548c\u89e3\u5305\u3002\u8fd9\u4e9b\u65b9\u6cd5\u8ba9\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u3001\u6613\u8bfb\u3002 [&hellip;]","protected":false},"author":3,"featured_media":1042915,"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\/1042907"}],"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=1042907"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1042907\/revisions"}],"predecessor-version":[{"id":1042917,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1042907\/revisions\/1042917"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1042915"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1042907"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1042907"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1042907"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}