{"id":939233,"date":"2024-12-26T20:30:42","date_gmt":"2024-12-26T12:30:42","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/939233.html"},"modified":"2024-12-26T20:30:44","modified_gmt":"2024-12-26T12:30:44","slug":"python%e5%a6%82%e4%bd%95%e7%bb%99%e5%ae%9a%e4%b9%89","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/939233.html","title":{"rendered":"python\u5982\u4f55\u7ed9\u5b9a\u4e49"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25074342\/875e941a-cb23-4ad6-b68b-bafb1efa974c.webp\" alt=\"python\u5982\u4f55\u7ed9\u5b9a\u4e49\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u7ed9\u53d8\u91cf\u5b9a\u4e49\u53ef\u4ee5\u901a\u8fc7\u76f4\u63a5\u8d4b\u503c\u3001\u5b9a\u4e49\u51fd\u6570\u3001\u5b9a\u4e49\u7c7b\u3001\u4f7f\u7528\u6a21\u5757\u7b49\u591a\u79cd\u65b9\u5f0f\u6765\u5b9e\u73b0\u3002<\/strong>\u5176\u4e2d\uff0c\u6700\u57fa\u7840\u7684\u65b9\u5f0f\u662f\u901a\u8fc7\u8d4b\u503c\u64cd\u4f5c\u6765\u5b9a\u4e49\u53d8\u91cf\uff0c\u7136\u540e\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49\u51fd\u6570\u6765\u5b9e\u73b0\u4ee3\u7801\u7684\u590d\u7528\u6027\uff0c\u518d\u8fdb\u4e00\u6b65\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49\u7c7b\u6765\u8fdb\u884c\u66f4\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\u548c\u884c\u4e3a\u7684\u5c01\u88c5\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u4e0d\u540c\u7684\u5b9a\u4e49\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u53d8\u91cf\u5b9a\u4e49<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5b9a\u4e49\u4e00\u4e2a\u53d8\u91cf\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u8981\u4f7f\u7528\u8d4b\u503c\u64cd\u4f5c\u5373\u53ef\u3002\u53d8\u91cf\u7684\u5b9a\u4e49\u4e0d\u9700\u8981\u4e8b\u5148\u58f0\u660e\u5176\u6570\u636e\u7c7b\u578b\uff0cPython\u4f1a\u6839\u636e\u8d4b\u503c\u7684\u5185\u5bb9\u81ea\u52a8\u63a8\u65ad\u7c7b\u578b\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u4e00\u4e2a\u6574\u6570\u53d8\u91cf<\/p>\n<p>age = 25<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a\u6d6e\u70b9\u6570\u53d8\u91cf<\/strong><\/h2>\n<p>height = 175.5<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a\u5b57\u7b26\u4e32\u53d8\u91cf<\/strong><\/h2>\n<p>name = &quot;Alice&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u53d8\u91cf\u7684\u547d\u540d\u9700\u8981\u9075\u5faa\u4e00\u5b9a\u7684\u89c4\u5219\uff1a\u53d8\u91cf\u540d\u5fc5\u987b\u4ee5\u5b57\u6bcd\u6216\u4e0b\u5212\u7ebf\u5f00\u5934\uff0c\u540e\u7eed\u53ef\u4ee5\u662f\u5b57\u6bcd\u3001\u6570\u5b57\u6216\u4e0b\u5212\u7ebf\u3002\u53d8\u91cf\u540d\u533a\u5206\u5927\u5c0f\u5199\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u51fd\u6570\u5b9a\u4e49<\/p>\n<\/p>\n<p><p>\u51fd\u6570\u662fPython\u4e2d\u5b9e\u73b0\u4ee3\u7801\u590d\u7528\u7684\u91cd\u8981\u624b\u6bb5\u3002\u5b9a\u4e49\u51fd\u6570\u4f7f\u7528 <code>def<\/code> \u5173\u952e\u5b57\uff0c\u540e\u9762\u8ddf\u4e0a\u51fd\u6570\u540d\u548c\u53c2\u6570\u5217\u8868\uff0c\u7136\u540e\u4f7f\u7528\u5192\u53f7 <code>:<\/code> \u8868\u793a\u51fd\u6570\u4f53\u7684\u5f00\u59cb\u3002\u51fd\u6570\u4f53\u7684\u4ee3\u7801\u9700\u8981\u7f29\u8fdb\u3002\u901a\u8fc7\u51fd\u6570\u7684\u5b9a\u4e49\uff0c\u53ef\u4ee5\u5c06\u5e38\u7528\u7684\u529f\u80fd\u5c01\u88c5\u8d77\u6765\uff0c\u65b9\u4fbf\u540e\u7eed\u8c03\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def greet(name):<\/p>\n<p>    &quot;&quot;&quot;\u6253\u5370\u95ee\u5019\u8bed&quot;&quot;&quot;<\/p>\n<p>    print(f&quot;Hello, {name}!&quot;)<\/p>\n<h2><strong>\u8c03\u7528\u51fd\u6570<\/strong><\/h2>\n<p>greet(&quot;Alice&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u51fd\u6570\u53ef\u4ee5\u6709\u8fd4\u56de\u503c\uff0c\u901a\u8fc7 <code>return<\/code> \u8bed\u53e5\u8fd4\u56de\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def add(a, b):<\/p>\n<p>    &quot;&quot;&quot;\u8fd4\u56de\u4e24\u4e2a\u6570\u7684\u548c&quot;&quot;&quot;<\/p>\n<p>    return a + b<\/p>\n<p>result = add(5, 3)<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1a8<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u7c7b\u5b9a\u4e49<\/p>\n<\/p>\n<p><p>Python\u662f\u4e00\u79cd\u9762\u5411\u5bf9\u8c61\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u7c7b\u662f\u9762\u5411\u5bf9\u8c61\u7684\u6838\u5fc3\u6982\u5ff5\u3002\u5b9a\u4e49\u7c7b\u4f7f\u7528 <code>class<\/code> \u5173\u952e\u5b57\uff0c\u540e\u9762\u8ddf\u4e0a\u7c7b\u540d\u548c\u5192\u53f7\u3002\u7c7b\u7684\u5b9a\u4e49\u5305\u62ec\u5c5e\u6027\u548c\u65b9\u6cd5\u3002\u5c5e\u6027\u901a\u5e38\u5728 <code>__init__<\/code> \u65b9\u6cd5\u4e2d\u5b9a\u4e49\uff0c\u800c\u65b9\u6cd5\u662f\u7c7b\u4e2d\u5b9a\u4e49\u7684\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Dog:<\/p>\n<p>    def __init__(self, name, age):<\/p>\n<p>        &quot;&quot;&quot;\u521d\u59cb\u5316\u5c5e\u6027&quot;&quot;&quot;<\/p>\n<p>        self.name = name<\/p>\n<p>        self.age = age<\/p>\n<p>    def sit(self):<\/p>\n<p>        &quot;&quot;&quot;\u6a21\u62df\u5c0f\u72d7\u5750\u4e0b&quot;&quot;&quot;<\/p>\n<p>        print(f&quot;{self.name} is now sitting.&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u5b9e\u4f8b<\/strong><\/h2>\n<p>my_dog = Dog(&quot;Willie&quot;, 6)<\/p>\n<h2><strong>\u8c03\u7528\u7c7b\u65b9\u6cd5<\/strong><\/h2>\n<p>my_dog.sit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u6a21\u5757\u548c\u5305<\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u597d\u5730\u7ec4\u7ec7\u4ee3\u7801\uff0cPython\u5141\u8bb8\u5c06\u76f8\u5173\u7684\u51fd\u6570\u548c\u7c7b\u653e\u5728\u4e00\u4e2a\u6a21\u5757\u4e2d\u3002\u6a21\u5757\u662f\u4e00\u4e2a\u5305\u542bPython\u4ee3\u7801\u7684\u6587\u4ef6\uff0c\u5305\u662f\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u6a21\u5757\u7684\u76ee\u5f55\u3002\u901a\u8fc7\u6a21\u5757\u548c\u5305\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u7ba1\u7406\u9879\u76ee\u7684\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># my_module.py<\/p>\n<p>def say_hello():<\/p>\n<p>    print(&quot;Hello from my module!&quot;)<\/p>\n<h2><strong>\u4f7f\u7528\u6a21\u5757<\/strong><\/h2>\n<p>import my_module<\/p>\n<p>my_module.say_hello()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u53d8\u91cf\u7684\u4f5c\u7528\u57df<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53d8\u91cf\u7684\u4f5c\u7528\u57df\u51b3\u5b9a\u4e86\u53d8\u91cf\u7684\u53ef\u89c1\u6027\u3002\u53d8\u91cf\u5206\u4e3a\u5168\u5c40\u53d8\u91cf\u548c\u5c40\u90e8\u53d8\u91cf\u3002\u5168\u5c40\u53d8\u91cf\u5728\u6574\u4e2a\u7a0b\u5e8f\u4e2d\u53ef\u89c1\uff0c\u800c\u5c40\u90e8\u53d8\u91cf\u53ea\u5728\u51fd\u6570\u6216\u4ee3\u7801\u5757\u4e2d\u53ef\u89c1\u3002\u4f7f\u7528 <code>global<\/code> \u5173\u952e\u5b57\u53ef\u4ee5\u5728\u51fd\u6570\u4e2d\u58f0\u660e\u5168\u5c40\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">x = 10  # \u5168\u5c40\u53d8\u91cf<\/p>\n<p>def change_global():<\/p>\n<p>    global x<\/p>\n<p>    x = 20  # \u4fee\u6539\u5168\u5c40\u53d8\u91cf<\/p>\n<p>change_global()<\/p>\n<p>print(x)  # \u8f93\u51fa\uff1a20<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516d\u3001\u52a8\u6001\u7c7b\u578b\u548c\u7c7b\u578b\u63d0\u793a<\/p>\n<\/p>\n<p><p>Python\u662f\u4e00\u79cd\u52a8\u6001\u7c7b\u578b\u7684\u8bed\u8a00\uff0c\u8fd9\u610f\u5473\u7740\u53d8\u91cf\u4e0d\u9700\u8981\u58f0\u660e\u7c7b\u578b\uff0c\u7c7b\u578b\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u6539\u53d8\u3002\u7136\u800c\uff0c\u4e3a\u4e86\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\uff0cPython 3.5\u5f15\u5165\u4e86\u7c7b\u578b\u63d0\u793a\u3002\u7c7b\u578b\u63d0\u793a\u4e0d\u5f71\u54cd\u7a0b\u5e8f\u8fd0\u884c\uff0c\u4f46\u53ef\u4ee5\u4e3a\u5f00\u53d1\u8005\u63d0\u4f9b\u66f4\u597d\u7684\u6587\u6863\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def add(a: int, b: int) -&gt; int:<\/p>\n<p>    return a + b<\/p>\n<p>result = add(2, 3)  # \u4f7f\u7528\u7c7b\u578b\u63d0\u793a<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e03\u3001\u5f02\u5e38\u5904\u7406<\/p>\n<\/p>\n<p><p>\u5728\u5b9a\u4e49\u51fd\u6570\u6216\u7c7b\u65f6\uff0c\u9700\u8981\u8003\u8651\u5f02\u5e38\u5904\u7406\u3002Python\u63d0\u4f9b\u4e86 <code>try-except<\/code> \u8bed\u53e5\u6765\u6355\u83b7\u548c\u5904\u7406\u5f02\u5e38\uff0c\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def divide(a, b):<\/p>\n<p>    try:<\/p>\n<p>        return a \/ b<\/p>\n<p>    except ZeroDivisionError:<\/p>\n<p>        print(&quot;Error: Division by zero is not allowed.&quot;)<\/p>\n<p>        return None<\/p>\n<p>result = divide(4, 0)  # \u5904\u7406\u9664\u96f6\u5f02\u5e38<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516b\u3001\u6587\u6863\u5b57\u7b26\u4e32<\/p>\n<\/p>\n<p><p>\u5728\u5b9a\u4e49\u51fd\u6570\u6216\u7c7b\u65f6\uff0c\u4f7f\u7528\u6587\u6863\u5b57\u7b26\u4e32\uff08docstring\uff09\u53ef\u4ee5\u4e3a\u4ee3\u7801\u6dfb\u52a0\u6587\u6863\u8bf4\u660e\u3002\u6587\u6863\u5b57\u7b26\u4e32\u662f\u4f4d\u4e8e\u51fd\u6570\u6216\u7c7b\u5b9a\u4e49\u4e0b\u65b9\u7684\u5b57\u7b26\u4e32\uff0c\u7528\u4e8e\u63cf\u8ff0\u529f\u80fd\u548c\u7528\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def multiply(a, b):<\/p>\n<p>    &quot;&quot;&quot;\u8fd4\u56de\u4e24\u4e2a\u6570\u7684\u4e58\u79ef&quot;&quot;&quot;<\/p>\n<p>    return a * b<\/p>\n<p>print(multiply.__doc__)  # \u8f93\u51fa\u6587\u6863\u5b57\u7b26\u4e32<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u5185\u5bb9\uff0c\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u4e86Python\u4e2d\u5b9a\u4e49\u53d8\u91cf\u3001\u51fd\u6570\u3001\u7c7b\u53ca\u5176\u76f8\u5173\u6982\u5ff5\u7684\u65b9\u6cd5\u3002\u8fd9\u4e9b\u65b9\u6cd5\u548c\u6982\u5ff5\u6784\u6210\u4e86Python\u7f16\u7a0b\u7684\u57fa\u7840\uff0c\u638c\u63e1\u5b83\u4eec\u80fd\u591f\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u7f16\u5199\u548c\u7ec4\u7ec7\u4ee3\u7801\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9a\u4e49\u53d8\u91cf\uff1f<\/strong><br \/>\u5728Python\u4e2d\u5b9a\u4e49\u53d8\u91cf\u975e\u5e38\u7b80\u5355\u3002\u60a8\u53ea\u9700\u9009\u62e9\u4e00\u4e2a\u540d\u79f0\u5e76\u4f7f\u7528\u8d4b\u503c\u8fd0\u7b97\u7b26\uff08=\uff09\u5c06\u5176\u503c\u8d4b\u7ed9\u53d8\u91cf\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u8fd9\u6837\u5b9a\u4e49\u4e00\u4e2a\u6574\u6570\u53d8\u91cf\uff1a<code>x = 10<\/code>\u3002\u53d8\u91cf\u540d\u53ef\u4ee5\u5305\u542b\u5b57\u6bcd\u3001\u6570\u5b57\u548c\u4e0b\u5212\u7ebf\uff0c\u4f46\u4e0d\u80fd\u4ee5\u6570\u5b57\u5f00\u5934\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u53ef\u4ee5\u5b9a\u4e49\u54ea\u4e9b\u7c7b\u578b\u7684\u6570\u636e\uff1f<\/strong><br \/>Python\u652f\u6301\u591a\u79cd\u6570\u636e\u7c7b\u578b\uff0c\u5305\u62ec\u6574\u6570\u3001\u6d6e\u70b9\u6570\u3001\u5b57\u7b26\u4e32\u3001\u5e03\u5c14\u503c\u3001\u5217\u8868\u3001\u5143\u7ec4\u3001\u96c6\u5408\u548c\u5b57\u5178\u7b49\u3002\u60a8\u53ef\u4ee5\u6839\u636e\u9700\u8981\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u7c7b\u578b\u6765\u5b9a\u4e49\u53d8\u91cf\u3002\u4f8b\u5982\uff0c\u5b57\u7b26\u4e32\u53ef\u4ee5\u7528\u5355\u5f15\u53f7\u6216\u53cc\u5f15\u53f7\u62ec\u8d77\u6765\uff1a<code>name = &quot;Alice&quot;<\/code>\u3002<\/p>\n<p><strong>\u5982\u4f55\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u5e76\u5728Python\u4e2d\u4f7f\u7528\u5b83\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>def<\/code>\u5173\u952e\u5b57\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u3002\u51fd\u6570\u7684\u540d\u79f0\u540e\u9762\u8ddf\u7740\u62ec\u53f7\uff0c\u62ec\u53f7\u5185\u53ef\u4ee5\u5b9a\u4e49\u53c2\u6570\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u51fd\u6570\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">def greet(name):  \n    return f&quot;Hello, {name}!&quot;  \n<\/code><\/pre>\n<p>\u4f7f\u7528<code>greet(&quot;Alice&quot;)<\/code>\u5c06\u8fd4\u56de<code>&quot;Hello, Alice!&quot;<\/code>\u3002\u51fd\u6570\u53ef\u4ee5\u5e2e\u52a9\u60a8\u7ec4\u7ec7\u4ee3\u7801\u5e76\u91cd\u590d\u4f7f\u7528\u903b\u8f91\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u7ed9\u53d8\u91cf\u5b9a\u4e49\u53ef\u4ee5\u901a\u8fc7\u76f4\u63a5\u8d4b\u503c\u3001\u5b9a\u4e49\u51fd\u6570\u3001\u5b9a\u4e49\u7c7b\u3001\u4f7f\u7528\u6a21\u5757\u7b49\u591a\u79cd\u65b9\u5f0f\u6765\u5b9e\u73b0\u3002\u5176\u4e2d\uff0c\u6700\u57fa\u7840\u7684\u65b9\u5f0f\u662f [&hellip;]","protected":false},"author":3,"featured_media":939236,"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\/939233"}],"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=939233"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/939233\/revisions"}],"predecessor-version":[{"id":939237,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/939233\/revisions\/939237"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/939236"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=939233"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=939233"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=939233"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}