{"id":3118,"date":"2025-05-29T16:00:25","date_gmt":"2025-06-04T08:53:25","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3118"},"modified":"2025-06-04T16:00:25","modified_gmt":"2025-06-04T08:53:25","slug":"python%e4%b8%ad%e7%9a%84%e5%8f%83%e6%95%b8%ef%bc%88parameters%ef%bc%89","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/python%e4%b8%ad%e7%9a%84%e5%8f%83%e6%95%b8%ef%bc%88parameters%ef%bc%89\/","title":{"rendered":"2025 \u6700\u65b0 Python \u53c3\u6578\u6aa2\u67e5\u6280\u5de7\uff1a\u4f7f\u7528 type hints \u548c\u51fd\u5f0f\u8a3b\u89e3"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, parameters, type hints, function annotations\"><\/p>\n<h1>2025 \u6700\u65b0 Python \u53c3\u6578\u6aa2\u67e5\u6280\u5de7<\/h1>\n<p>\u5728 Python \u4e2d\uff0c\u51fd\u5f0f\u7684\u53c3\u6578\u6aa2\u67e5\u662f\u4e00\u500b\u91cd\u8981\u7684\u4e3b\u984c\u3002\u96a8\u8457 Python \u8a9e\u8a00\u7684\u9032\u6b65\uff0c\u5f9e 2025 \u958b\u59cb\uff0c\u4f7f\u7528 **type hints**\uff08\u985e\u578b\u63d0\u793a\uff09\u548c\u51fd\u5f0f\u8a3b\u89e3\u6210\u70ba\u6aa2\u67e5\u53c3\u6578\u7684\u6700\u4f73\u5be6\u8e10\u3002\u9019\u4e0d\u50c5\u80fd\u63d0\u9ad8\u7a0b\u5f0f\u7684\u53ef\u8b80\u6027\uff0c\u9084\u80fd\u5728\u958b\u767c\u904e\u7a0b\u4e2d\u63d0\u4f9b\u66f4\u597d\u7684\u932f\u8aa4\u6aa2\u67e5\u3002<\/p>\n<h2>\u4f7f\u7528 type hints \u9032\u884c\u53c3\u6578\u6aa2\u67e5<\/h2>\n<p>\u985e\u578b\u63d0\u793a\u5141\u8a31\u958b\u767c\u8005\u70ba\u51fd\u5f0f\u7684\u53c3\u6578\u548c\u8fd4\u56de\u503c\u6307\u5b9a\u985e\u578b\uff0c\u9019\u6709\u52a9\u65bc\u975c\u614b\u6aa2\u67e5\u5de5\u5177\uff08\u5982 `mypy`\uff09\u5728\u958b\u767c\u904e\u7a0b\u4e2d\u63d0\u524d\u767c\u73fe\u932f\u8aa4\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528 type hints \u7684\u7bc4\u4f8b\uff1a<\/p>\n<pre class=\"brush: python\">\ndef my_function(x: int, y: int) -> int:\n    return x + y\n<\/pre>\n<p>\u5728\u9019\u500b\u7bc4\u4f8b\u4e2d\uff0c\u6211\u5011\u660e\u78ba\u6307\u5b9a `x` \u548c `y` \u53c3\u6578\u7684\u985e\u578b\u70ba `int`\uff0c\u4e26\u4e14\u51fd\u5f0f\u8fd4\u56de\u503c\u7684\u985e\u578b\u4e5f\u662f `int`\u3002<\/p>\n<h2>\u4f7f\u7528 assert \u9032\u884c\u503c\u7684\u6aa2\u67e5<\/h2>\n<p>\u96d6\u7136 type hints \u63d0\u4f9b\u4e86\u985e\u578b\u6aa2\u67e5\u7684\u529f\u80fd\uff0c\u4f46\u5b83\u4e26\u4e0d\u6703\u5728\u904b\u884c\u6642\u6aa2\u67e5\u53c3\u6578\u7684\u503c\u3002\u70ba\u4e86\u9032\u884c\u66f4\u6df1\u5165\u7684\u6aa2\u67e5\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528 `assert` \u8a9e\u53e5\u4f86\u9a57\u8b49\u53c3\u6578\u7684\u503c\u662f\u5426\u7b26\u5408\u8981\u6c42\u3002\u4f8b\u5982\uff1a<\/p>\n<pre class=\"brush: python\">\ndef my_function(x: int, y: int) -> int:\n    assert x > 0, \"x \u5fc5\u9808\u5927\u65bc 0\"\n    assert y > 0, \"y \u5fc5\u9808\u5927\u65bc 0\"\n    return x + y\n<\/pre>\n<p>\u9019\u6a23\uff0c\u5728\u51fd\u5f0f\u8abf\u7528\u6642\uff0c\u5982\u679c `x` \u6216 `y` \u7684\u503c\u4e0d\u7b26\u5408\u8981\u6c42\uff0c\u5c31\u6703\u5f15\u767c\u932f\u8aa4\uff0c\u5e6b\u52a9\u6211\u5011\u5feb\u901f\u5b9a\u4f4d\u554f\u984c\u3002<\/p>\n<h2>\u5be6\u4f5c\u7bc4\u4f8b\uff1a\u5b8c\u6574\u7684\u53c3\u6578\u6aa2\u67e5<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u5b8c\u6574\u7684\u7bc4\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u7d50\u5408 type hints \u548c assert \u8a9e\u53e5\u9032\u884c\u53c3\u6578\u6aa2\u67e5\uff1a<\/p>\n<pre class=\"brush: python\">\ndef calculate_area(length: float, width: float) -> float:\n    assert length > 0, \"\u9577\u5ea6\u5fc5\u9808\u5927\u65bc 0\"\n    assert width > 0, \"\u5bec\u5ea6\u5fc5\u9808\u5927\u65bc 0\"\n    return length * width\n\n# \u7bc4\u4f8b\u8abf\u7528\ntry:\n    area = calculate_area(5.0, 3.0)\n    print(f\"\u9762\u7a4d: {area}\")\nexcept AssertionError as e:\n    print(e)\n<\/pre>\n<p>\u9019\u500b\u7bc4\u4f8b\u4e2d\uff0c\u6211\u5011\u8a08\u7b97\u77e9\u5f62\u7684\u9762\u7a4d\uff0c\u540c\u6642\u6aa2\u67e5\u9577\u5ea6\u548c\u5bec\u5ea6\u662f\u5426\u70ba\u6b63\u6578\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528\uff1a\u4f7f\u7528\u88dd\u98fe\u5668\u9032\u884c\u53c3\u6578\u6aa2\u67e5<\/h2>\n<p>\u5982\u679c\u4f60\u60f3\u8981\u66f4\u901a\u7528\u7684\u89e3\u6c7a\u65b9\u6848\uff0c\u53ef\u4ee5\u8003\u616e\u4f7f\u7528\u88dd\u98fe\u5668\u4f86\u9032\u884c\u53c3\u6578\u6aa2\u67e5\u3002\u88dd\u98fe\u5668\u5141\u8a31\u4f60\u5728\u4e0d\u4fee\u6539\u51fd\u5f0f\u672c\u8eab\u7684\u60c5\u6cc1\u4e0b\u6dfb\u52a0\u984d\u5916\u7684\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u88dd\u98fe\u5668\u7bc4\u4f8b\uff1a<\/p>\n<pre class=\"brush: python\">\nfrom functools import wraps\n\ndef validate_positive(func):\n    @wraps(func)\n    def wrapper(*args, **kwargs):\n        for arg in args:\n            assert arg > 0, f\"\u53c3\u6578 {arg} \u5fc5\u9808\u5927\u65bc 0\"\n        return func(*args, **kwargs)\n    return wrapper\n\n@validate_positive\ndef add(x: int, y: int) -> int:\n    return x + y\n<\/pre>\n<p>\u5728\u9019\u500b\u7bc4\u4f8b\u4e2d\uff0c`validate_positive` \u88dd\u98fe\u5668\u5c07\u6aa2\u67e5\u6240\u6709\u4f4d\u7f6e\u53c3\u6578\u662f\u5426\u70ba\u6b63\u6578\u3002<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>\u5728 2025 \u5e74\uff0cPython \u63d0\u4f9b\u4e86\u591a\u7a2e\u65b9\u5f0f\u4f86\u6aa2\u67e5\u51fd\u5f0f\u7684\u53c3\u6578\uff0c\u5305\u62ec\u4f7f\u7528 type hints\u3001assert \u8a9e\u53e5\u548c\u88dd\u98fe\u5668\u3002\u9019\u4e9b\u5de5\u5177\u4e0d\u50c5\u63d0\u9ad8\u4e86\u7a0b\u5f0f\u7684\u5065\u58ef\u6027\uff0c\u4e5f\u4f7f\u5f97\u7a0b\u5f0f\u7684\u53ef\u8b80\u6027\u5927\u5927\u589e\u5f37\u3002\u96a8\u8457 Python \u7684\u4e0d\u65b7\u6f14\u9032\uff0c\u5b78\u6703\u9019\u4e9b\u6700\u4f73\u5be6\u8e10\u5c07\u6709\u52a9\u65bc\u63d0\u5347\u4f60\u7684\u958b\u767c\u6280\u80fd\u3002<\/p>\n<p>\u5982\u9700\u9032\u4e00\u6b65\u4e86\u89e3 Python \u7684\u5404\u7a2e\u6280\u5de7\u8207\u5be6\u4f5c\uff0c\u8acb\u53c3\u8003 [vocus.cc \u7684 Python \u6559\u5b78\u6587\u7ae0](https:\/\/vocus.cc) \u6216 [miner.tw \u7684\u76f8\u95dc\u8cc7\u6e90](https:\/\/miner.tw)\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<p>**Q1: Python \u7684 type hints \u662f\u4ec0\u9ebc\uff1f**<br \/>\nA1: Type hints \u662f\u4e00\u7a2e\u5728 Python \u51fd\u5f0f\u4e2d\u6307\u5b9a\u53c3\u6578\u548c\u8fd4\u56de\u503c\u985e\u578b\u7684\u65b9\u5f0f\uff0c\u5e6b\u52a9\u63d0\u9ad8\u7a0b\u5f0f\u7684\u53ef\u8b80\u6027\u548c\u975c\u614b\u6aa2\u67e5\u7684\u6e96\u78ba\u6027\u3002<\/p>\n<p>**Q2: \u5982\u4f55\u6aa2\u67e5\u51fd\u5f0f\u53c3\u6578\u7684\u503c\uff1f**<br \/>\nA2: \u53ef\u4ee5\u4f7f\u7528 `assert` \u8a9e\u53e5\u6aa2\u67e5\u53c3\u6578\u7684\u503c\u662f\u5426\u7b26\u5408\u7279\u5b9a\u689d\u4ef6\uff0c\u82e5\u689d\u4ef6\u4e0d\u6eff\u8db3\u5247\u6703\u5f15\u767c\u932f\u8aa4\u3002<\/p>\n<p>**Q3: \u88dd\u98fe\u5668\u5982\u4f55\u904b\u4f5c\uff1f**<br \/>\nA3: \u88dd\u98fe\u5668\u662f\u4e00\u7a2e\u51fd\u5f0f\uff0c\u5141\u8a31\u4f60\u5728\u4e0d\u4fee\u6539\u539f\u6709\u51fd\u5f0f\u7684\u60c5\u6cc1\u4e0b\u589e\u52a0\u984d\u5916\u7684\u6aa2\u67e5\u6216\u529f\u80fd\uff0c\u901a\u5e38\u7528\u65bc\u91cd\u8907\u6027\u4efb\u52d9\u7684\u7c21\u5316\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python\u4e2d\u7684\u53c3\u6578\uff08Parameters\uff09\u662f\u4e00\u7a2e\u7c21\u55ae\u800c\u6709\u6548\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8b93\u60a8\u66f4\u8f15\u9b06\u5730\u63a7\u5236\u7a0b\u5e8f\u7684\u884c\u70ba\u3002\u672c\u6587\u5c07\u4ecb\u7d39Python\u4e2d\u7684\u53c3\u6578\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u6709\u7528\u7684\u6280\u5de7\uff0c\u4ee5\u5e6b\u52a9\u60a8\u66f4\u597d\u5730\u4f7f\u7528\u5b83\u5011\u3002<\/p>\n","protected":false},"author":1,"featured_media":2518,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[18],"tags":[15],"class_list":["post-3118","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-python"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2022\/12\/DALL\u00b7E-2022-12-28-14.25.55-\u62f7\u8c9d2.png","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-Oi","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3118","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=3118"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3118\/revisions"}],"predecessor-version":[{"id":3119,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3118\/revisions\/3119"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/2518"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=3118"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3118"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3118"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}