{"id":1100018,"date":"2025-01-08T15:36:11","date_gmt":"2025-01-08T07:36:11","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1100018.html"},"modified":"2025-01-08T15:36:13","modified_gmt":"2025-01-08T07:36:13","slug":"%e5%a6%82%e4%bd%95%e6%9f%a5%e7%9c%8bpython%e7%9a%84%e5%87%bd%e6%95%b0%e7%9a%84%e5%8f%82%e6%95%b0%e7%b1%bb%e5%9e%8b-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1100018.html","title":{"rendered":"\u5982\u4f55\u67e5\u770bpython\u7684\u51fd\u6570\u7684\u53c2\u6570\u7c7b\u578b"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25063450\/09f8c56b-5d18-474f-9f88-d4f62bf785f4.webp\" alt=\"\u5982\u4f55\u67e5\u770bpython\u7684\u51fd\u6570\u7684\u53c2\u6570\u7c7b\u578b\" \/><\/p>\n<p><p> <strong>\u8981\u67e5\u770bPython\u51fd\u6570\u7684\u53c2\u6570\u7c7b\u578b\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a\u6ce8\u91ca\u3001type hints\u3001inspect\u6a21\u5757\u3002<\/strong> \u5176\u4e2d\uff0c<strong>\u4f7f\u7528type hints<\/strong>\u662f\u63a8\u8350\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u76f4\u63a5\u5728\u4ee3\u7801\u4e2d\u6807\u660e\u53c2\u6570\u7c7b\u578b\uff0c\u65b9\u4fbf\u9605\u8bfb\u548c\u7ef4\u62a4\u3002\u63a5\u4e0b\u6765\u6211\u4eec\u8be6\u7ec6\u8ba8\u8bba\u8fd9\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528type hints\uff0c\u4f60\u53ef\u4ee5\u5728\u51fd\u6570\u5b9a\u4e49\u65f6\u4f7f\u7528\u6ce8\u89e3\u7684\u5f62\u5f0f\u6765\u6807\u660e\u53c2\u6570\u7c7b\u578b\u548c\u8fd4\u56de\u503c\u7c7b\u578b\u3002\u8fd9\u6837\u4e0d\u4ec5\u5728\u4ee3\u7801\u9605\u8bfb\u65f6\u975e\u5e38\u76f4\u89c2\uff0c\u8fd8\u80fd\u901a\u8fc7\u4e00\u4e9b\u5de5\u5177\uff08\u5982mypy\uff09\u8fdb\u884c\u9759\u6001\u7c7b\u578b\u68c0\u67e5\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u5065\u58ee\u6027\u548c\u53ef\u7ef4\u62a4\u6027\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><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c<code>a<\/code>\u548c<code>b<\/code>\u88ab\u6807\u6ce8\u4e3a\u6574\u6570\u7c7b\u578b\uff0c\u51fd\u6570\u7684\u8fd4\u56de\u503c\u4e5f\u662f\u6574\u6570\u7c7b\u578b\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u660e\u786e\u51fd\u6570\u7684\u53c2\u6570\u7c7b\u578b\u548c\u8fd4\u56de\u503c\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u8ba8\u8bba\u5176\u4ed6\u65b9\u6cd5\uff0c\u5e76\u5c55\u793a\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u6765\u67e5\u770b\u548c\u9a8c\u8bc1Python\u51fd\u6570\u7684\u53c2\u6570\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u6ce8\u91ca<\/h3>\n<\/p>\n<p><p>\u5728Python 3.5\u4e4b\u524d\uff0cPython\u5e76\u4e0d\u652f\u6301\u539f\u751f\u7684\u7c7b\u578b\u6ce8\u89e3\uff0c\u6240\u4ee5\u6211\u4eec\u901a\u5e38\u4f1a\u4f7f\u7528\u6ce8\u91ca\u6765\u8bf4\u660e\u51fd\u6570\u53c2\u6570\u548c\u8fd4\u56de\u503c\u7684\u7c7b\u578b\u3002\u8fd9\u79cd\u65b9\u6cd5\u867d\u7136\u7b80\u5355\uff0c\u4f46\u5e76\u6ca1\u6709\u5b9e\u9645\u7684\u7ea6\u675f\u529b\uff0c\u53ea\u662f\u4f5c\u4e3a\u4e00\u79cd\u4ee3\u7801\u6ce8\u91ca\uff0c\u5e2e\u52a9\u5176\u4ed6\u5f00\u53d1\u8005\u7406\u89e3\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def add(a, b):<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    :param a: int<\/p>\n<p>    :param b: int<\/p>\n<p>    :return: int<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    return a + b<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u867d\u7136\u6613\u4e8e\u5b9e\u73b0\uff0c\u4f46\u5728\u4ee3\u7801\u7ef4\u62a4\u4e0a\u663e\u5f97\u4e0d\u591f\u76f4\u89c2\uff0c\u4e5f\u6ca1\u6709\u5b9e\u9645\u7684\u7c7b\u578b\u7ea6\u675f\u548c\u9759\u6001\u68c0\u67e5\u80fd\u529b\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528type hints<\/h3>\n<\/p>\n<p><p>Python 3.5\u5f15\u5165\u4e86\u7c7b\u578b\u6ce8\u89e3\uff0c\u4f7f\u5f97\u6211\u4eec\u53ef\u4ee5\u5728\u51fd\u6570\u5b9a\u4e49\u65f6\u76f4\u63a5\u6807\u6ce8\u53c2\u6570\u548c\u8fd4\u56de\u503c\u7c7b\u578b\u3002\u8fd9\u4e0d\u4ec5\u63d0\u9ad8\u4e86\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u9759\u6001\u7c7b\u578b\u68c0\u67e5\u5de5\u5177\uff08\u5982mypy\uff09\u6765\u9a8c\u8bc1\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def multiply(x: float, y: float) -&gt; float:<\/p>\n<p>    return x * y<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>x<\/code>\u548c<code>y<\/code>\u7684\u7c7b\u578b\u88ab\u6807\u6ce8\u4e3a\u6d6e\u70b9\u578b\uff0c\u51fd\u6570\u7684\u8fd4\u56de\u503c\u4e5f\u662f\u6d6e\u70b9\u578b\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u8ba9\u4ee3\u7801\u66f4\u52a0\u81ea\u6587\u6863\u5316\uff0c\u5e76\u4e14\u5728\u4e00\u5b9a\u7a0b\u5ea6\u4e0a\u63d0\u9ad8\u4e86\u4ee3\u7801\u7684\u5065\u58ee\u6027\u3002<\/p>\n<\/p>\n<p><h4>\u7ed3\u5408mypy\u8fdb\u884c\u9759\u6001\u68c0\u67e5<\/h4>\n<\/p>\n<p><p>\u5b89\u88c5mypy\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pip install mypy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884cmypy\u8fdb\u884c\u7c7b\u578b\u68c0\u67e5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">mypy your_script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7mypy\u53ef\u4ee5\u63d0\u524d\u53d1\u73b0\u4ee3\u7801\u4e2d\u7684\u7c7b\u578b\u9519\u8bef\uff0c\u907f\u514d\u5728\u8fd0\u884c\u65f6\u51fa\u73b0\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528inspect\u6a21\u5757<\/h3>\n<\/p>\n<p><p>Python\u7684<code>inspect<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5f3a\u5927\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u7528\u6765\u83b7\u53d6\u51fd\u6570\u7684\u53c2\u6570\u4fe1\u606f\u3002<code>inspect.signature<\/code>\u51fd\u6570\u53ef\u4ee5\u83b7\u53d6\u51fd\u6570\u7684\u7b7e\u540d\uff0c\u5305\u62ec\u53c2\u6570\u540d\u3001\u9ed8\u8ba4\u503c\u548c\u6ce8\u89e3\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import inspect<\/p>\n<p>def example_function(a: int, b: str = &quot;default&quot;) -&gt; bool:<\/p>\n<p>    return str(a) == b<\/p>\n<p>signature = inspect.signature(example_function)<\/p>\n<p>for param in signature.parameters.values():<\/p>\n<p>    print(f&quot;Name: {param.name}, Annotation: {param.annotation}, Default: {param.default}&quot;)<\/p>\n<p>print(f&quot;Return annotation: {signature.return_annotation}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570<code>example_function<\/code>\uff0c\u5e76\u4f7f\u7528<code>inspect.signature<\/code>\u83b7\u53d6\u5176\u7b7e\u540d\u4fe1\u606f\u3002\u901a\u8fc7\u904d\u5386<code>signature.parameters<\/code>\uff0c\u53ef\u4ee5\u83b7\u53d6\u6bcf\u4e2a\u53c2\u6570\u7684\u540d\u79f0\u3001\u6ce8\u89e3\u548c\u9ed8\u8ba4\u503c\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u83b7\u53d6\u51fd\u6570\u7684\u8fd4\u56de\u503c\u6ce8\u89e3\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u65b9\u6cd5\uff0cPython\u793e\u533a\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u7b2c\u4e09\u65b9\u5e93\u6765\u5e2e\u52a9\u6211\u4eec\u67e5\u770b\u548c\u9a8c\u8bc1\u51fd\u6570\u53c2\u6570\u7684\u7c7b\u578b\u3002\u4f8b\u5982\uff0c<code>typeguard<\/code>\u5e93\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u68c0\u67e5\u51fd\u6570\u53c2\u6570\u548c\u8fd4\u56de\u503c\u7684\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><p>\u5b89\u88c5<code>typeguard<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pip install typeguard<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528<code>typeguard<\/code>\u7684<code>@typechecked<\/code>\u88c5\u9970\u5668\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from typeguard import typechecked<\/p>\n<p>@typechecked<\/p>\n<p>def divide(a: int, b: int) -&gt; float:<\/p>\n<p>    return a \/ b<\/p>\n<p>divide(4, 2)  # \u6b63\u5e38\u6267\u884c<\/p>\n<p>divide(4, &quot;2&quot;)  # \u629b\u51faTypeError<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u5bf9\u51fd\u6570\u53c2\u6570\u548c\u8fd4\u56de\u503c\u8fdb\u884c\u7c7b\u578b\u68c0\u67e5\uff0c\u8fdb\u4e00\u6b65\u63d0\u9ad8\u4ee3\u7801\u7684\u5065\u58ee\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u67e5\u770b\u548c\u9a8c\u8bc1Python\u51fd\u6570\u7684\u53c2\u6570\u7c7b\u578b\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u6ce8\u91ca\u3001type hints\u3001inspect\u6a21\u5757\u548c\u7b2c\u4e09\u65b9\u5e93\u3002<strong>\u63a8\u8350\u4f7f\u7528type hints<\/strong>\uff0c\u56e0\u4e3a\u5b83\u4e0d\u4ec5\u7b80\u5355\u76f4\u89c2\uff0c\u8fd8\u53ef\u4ee5\u7ed3\u5408\u9759\u6001\u7c7b\u578b\u68c0\u67e5\u5de5\u5177\uff08\u5982mypy\uff09\u8fdb\u884c\u9759\u6001\u68c0\u67e5\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u5065\u58ee\u6027\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u4e0d\u4ec5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\uff0c\u8fd8\u80fd\u5728\u5f00\u53d1\u9636\u6bb5\u63d0\u524d\u53d1\u73b0\u6f5c\u5728\u7684\u95ee\u9898\uff0c\u907f\u514d\u5728\u8fd0\u884c\u65f6\u51fa\u73b0\u7c7b\u578b\u9519\u8bef\u3002\u5e0c\u671b\u901a\u8fc7\u672c\u6587\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u67e5\u770b\u548c\u7ba1\u7406Python\u51fd\u6570\u7684\u53c2\u6570\u7c7b\u578b\uff0c\u4ece\u800c\u7f16\u5199\u51fa\u66f4\u52a0\u5065\u58ee\u548c\u9ad8\u6548\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u67e5\u770b\u51fd\u6570\u7684\u53c2\u6570\u7c7b\u578b\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>inspect<\/code>\u6a21\u5757\u6765\u67e5\u770b\u51fd\u6570\u7684\u53c2\u6570\u7c7b\u578b\u3002\u901a\u8fc7\u8c03\u7528<code>inspect.signature()<\/code>\uff0c\u60a8\u53ef\u4ee5\u83b7\u53d6\u51fd\u6570\u7684\u7b7e\u540d\uff0c\u5e76\u4e14\u53ef\u4ee5\u4ece\u4e2d\u63d0\u53d6\u53c2\u6570\u7684\u8be6\u7ec6\u4fe1\u606f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">import inspect\n\ndef example_function(a: int, b: str, c: float = 1.0):\n    pass\n\nsig = inspect.signature(example_function)\nfor param in sig.parameters.values():\n    print(f&quot;Parameter: {param.name}, Type: {param.annotation or &#39;No type hint&#39;}&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u5e2e\u52a9\u60a8\u4e86\u89e3\u51fd\u6570\u7684\u53c2\u6570\u7c7b\u578b\u53ca\u5176\u9ed8\u8ba4\u503c\u3002<\/p>\n<p><strong>\u4f7f\u7528\u7c7b\u578b\u63d0\u793a\u7684\u597d\u5904\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u7c7b\u578b\u63d0\u793a\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u901a\u8fc7\u6307\u5b9a\u51fd\u6570\u53c2\u6570\u548c\u8fd4\u56de\u503c\u7684\u7c7b\u578b\uff0c\u5176\u4ed6\u5f00\u53d1\u8005\u80fd\u591f\u5feb\u901f\u7406\u89e3\u51fd\u6570\u7684\u7528\u6cd5\u548c\u9884\u671f\u8f93\u5165\u3002\u8fd9\u4e5f\u6709\u52a9\u4e8e\u9759\u6001\u7c7b\u578b\u68c0\u67e5\u5de5\u5177\uff08\u5982mypy\uff09\u53d1\u73b0\u6f5c\u5728\u7684\u9519\u8bef\uff0c\u589e\u5f3a\u4ee3\u7801\u7684\u5065\u58ee\u6027\u3002<\/p>\n<p><strong>\u5728\u6ca1\u6709\u7c7b\u578b\u63d0\u793a\u7684\u60c5\u51b5\u4e0b\uff0c\u5982\u4f55\u786e\u5b9a\u53c2\u6570\u7684\u7c7b\u578b\uff1f<\/strong><br \/>\u5982\u679c\u51fd\u6570\u672a\u4f7f\u7528\u7c7b\u578b\u63d0\u793a\uff0c\u60a8\u4ecd\u7136\u53ef\u4ee5\u901a\u8fc7\u5728\u51fd\u6570\u5185\u90e8\u4f7f\u7528<code>type()<\/code>\u51fd\u6570\u6765\u68c0\u67e5\u53c2\u6570\u7684\u7c7b\u578b\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">def example_function(a, b):\n    print(type(a), type(b))\n\nexample_function(10, &quot;hello&quot;)  # \u8f93\u51fa: &lt;class &#39;int&#39;&gt; &lt;class &#39;str&#39;&gt;\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u867d\u7136\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u67e5\u770b\u53c2\u6570\u7684\u7c7b\u578b\uff0c\u4f46\u4e0d\u5982\u7c7b\u578b\u63d0\u793a\u76f4\u89c2\u4e14\u6613\u4e8e\u7406\u89e3\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u67e5\u770bPython\u51fd\u6570\u7684\u53c2\u6570\u7c7b\u578b\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a\u6ce8\u91ca\u3001type hints\u3001inspect\u6a21\u5757\u3002 \u5176\u4e2d\uff0c\u4f7f [&hellip;]","protected":false},"author":3,"featured_media":1100025,"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\/1100018"}],"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=1100018"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1100018\/revisions"}],"predecessor-version":[{"id":1100028,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1100018\/revisions\/1100028"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1100025"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1100018"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1100018"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1100018"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}