{"id":3332,"date":"2025-05-30T16:11:23","date_gmt":"2025-06-04T08:12:22","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3332"},"modified":"2025-06-04T16:11:23","modified_gmt":"2025-06-04T08:12:22","slug":"python%e4%b8%ad%e7%9a%84annotations%ef%bc%9a%e7%b0%a1%e5%96%ae%e6%98%93%e6%87%82%e7%9a%84%e4%bb%8b%e7%b4%b9","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/python%e4%b8%ad%e7%9a%84annotations%ef%bc%9a%e7%b0%a1%e5%96%ae%e6%98%93%e6%87%82%e7%9a%84%e4%bb%8b%e7%b4%b9\/","title":{"rendered":"2025 \u6700\u65b0 Python Annotations \u6559\u5b78\uff1a\u5f37\u5316\u7a0b\u5f0f\u78bc\u53ef\u8b80\u6027\u8207\u6aa2\u67e5\u578b\u614b"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, annotations, Python \u7a0b\u5f0f\u6559\u5b78, \u578b\u614b\u8a3b\u89e3\"><\/p>\n<h1>Python Annotations \u529f\u80fd\u6df1\u5ea6\u89e3\u6790<\/h1>\n<p>Python \u7684 Annotations \u529f\u80fd\u662f\u4e00\u500b\u975e\u5e38\u5be6\u7528\u7684\u7279\u6027\uff0c\u8b93\u958b\u767c\u8005\u5728\u5b9a\u7fa9\u51fd\u5f0f\u6642\uff0c\u53ef\u4ee5\u5c0d\u53c3\u6578\u548c\u56de\u50b3\u503c\u9032\u884c\u578b\u614b\u8a3b\u89e3\uff0c\u9019\u4e0d\u50c5\u63d0\u5347\u4e86\u7a0b\u5f0f\u78bc\u7684\u53ef\u8b80\u6027\uff0c\u4e5f\u6709\u52a9\u65bc\u975c\u614b\u6aa2\u67e5\u5de5\u5177\u6aa2\u6e2c\u578b\u614b\u932f\u8aa4\u3002<\/p>\n<h2>Annotations \u7684\u57fa\u672c\u8a9e\u6cd5<\/h2>\n<p>Annotations \u7684\u8a9e\u6cd5\u76f8\u7576\u7c21\u55ae\uff0c\u958b\u767c\u8005\u53ea\u9700\u5728\u51fd\u5f0f\u5b9a\u7fa9\u4e2d\uff0c\u5c0d\u53c3\u6578\u548c\u56de\u50b3\u503c\u4f7f\u7528\u5192\u865f\uff08:\uff09\u6a19\u793a\u578b\u614b\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7bc4\u4f8b\uff0c\u986f\u793a\u5982\u4f55\u4f7f\u7528 Annotations \u5b9a\u7fa9\u4e00\u500b\u63a5\u6536\u6574\u6578\u4e26\u56de\u50b3\u5b57\u4e32\u7684\u51fd\u5f0f\uff1a<\/p>\n<p>&#8220;`python<br \/>\ndef my_func(num: int) -> str:<br \/>\n    return str(num)<br \/>\n&#8220;`<\/p>\n<p>\u5728\u9019\u500b\u7bc4\u4f8b\u4e2d\uff0c`num` \u53c3\u6578\u88ab\u8a3b\u89e3\u70ba\u6574\u6578\u578b\u614b\uff0c\u800c\u51fd\u5f0f\u7684\u56de\u50b3\u578b\u614b\u88ab\u6a19\u793a\u70ba\u5b57\u4e32\u578b\u614b\u3002\u9019\u6a23\u7684\u505a\u6cd5\u4e0d\u50c5\u4f7f\u7a0b\u5f0f\u78bc\u66f4\u5177\u53ef\u8b80\u6027\uff0c\u4e5f\u4fbf\u65bc\u5176\u4ed6\u958b\u767c\u8005\u7406\u89e3\u51fd\u5f0f\u7684\u7528\u9014\u3002<\/p>\n<h2>Annotations \u7684\u512a\u9ede<\/h2>\n<p>\u4f7f\u7528 Annotations \u6709\u591a\u500b\u512a\u9ede\uff1a<\/p>\n<p>1. **\u63d0\u9ad8\u53ef\u8b80\u6027**\uff1a\u900f\u904e\u578b\u614b\u8a3b\u89e3\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u8fc5\u901f\u7406\u89e3\u51fd\u5f0f\u7684\u53c3\u6578\u548c\u56de\u50b3\u503c\u7684\u9810\u671f\u578b\u614b\u3002<\/p>\n<p>2. **\u975c\u614b\u578b\u614b\u6aa2\u67e5**\uff1a\u5373\u4f7f Python \u662f\u5f31\u578b\u5225\u8a9e\u8a00\uff0cAnnotations \u53ef\u4ee5\u8207\u975c\u614b\u578b\u614b\u6aa2\u67e5\u5de5\u5177\uff08\u5982 `mypy`\uff09\u7d50\u5408\uff0c\u5e6b\u52a9\u958b\u767c\u8005\u53ca\u65e9\u767c\u73fe\u578b\u614b\u932f\u8aa4\u3002<\/p>\n<p>3. **\u6539\u5584 IDE \u652f\u63f4**\uff1a\u8a31\u591a\u73fe\u4ee3\u7684 IDE \u90fd\u80fd\u6839\u64da Annotations \u63d0\u4f9b\u66f4\u7cbe\u78ba\u7684\u81ea\u52d5\u88dc\u5168\u548c\u932f\u8aa4\u63d0\u793a\uff0c\u9032\u4e00\u6b65\u63d0\u5347\u958b\u767c\u6548\u7387\u3002<\/p>\n<h2>Annotations \u7684\u9650\u5236<\/h2>\n<p>\u5118\u7ba1 Annotations \u6709\u8a31\u591a\u512a\u9ede\uff0c\u4f46\u4e5f\u5b58\u5728\u4e00\u4e9b\u9650\u5236\uff1a<\/p>\n<p>1. **\u4e0d\u652f\u63f4\u8b8a\u6578\u8a3b\u89e3**\uff1aAnnotations \u53ea\u80fd\u7528\u65bc\u51fd\u5f0f\u7684\u53c3\u6578\u548c\u56de\u50b3\u503c\uff0c\u7121\u6cd5\u5c0d\u8b8a\u6578\u9032\u884c\u578b\u614b\u8a3b\u89e3\u3002<\/p>\n<p>2. **\u7121\u6cd5\u5f37\u5236\u578b\u614b\u6aa2\u67e5**\uff1aAnnotations \u672c\u8eab\u4e0d\u6703\u5f37\u5236\u57f7\u884c\u578b\u614b\u6aa2\u67e5\uff0c\u958b\u767c\u8005\u4ecd\u9700\u5728\u7a0b\u5f0f\u78bc\u4e2d\u81ea\u884c\u8655\u7406\u578b\u614b\u9a57\u8b49\u3002<\/p>\n<h2>\u5be6\u4f5c\u7bc4\u4f8b\uff1a\u63d0\u9ad8\u7a0b\u5f0f\u78bc\u7684\u53ef\u8b80\u6027\u8207\u5b89\u5168\u6027<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u5b8c\u6574\u7684\u7bc4\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u904b\u7528 Annotations \u9032\u884c\u578b\u614b\u8a3b\u89e3\uff0c\u4e26\u7d50\u5408\u7c21\u55ae\u7684\u578b\u614b\u6aa2\u67e5\uff1a<\/p>\n<p>&#8220;`python<br \/>\nfrom typing import List<\/p>\n<p>def calculate_average(numbers: List[int]) -> float:<br \/>\n    if not numbers:<br \/>\n        raise ValueError(&#8220;\u6578\u5217\u4e0d\u80fd\u70ba\u7a7a&#8221;)<br \/>\n    return sum(numbers) \/ len(numbers)<\/p>\n<p># \u4f7f\u7528\u7bc4\u4f8b<br \/>\ntry:<br \/>\n    avg = calculate_average([10, 20, 30])<br \/>\n    print(f&#8221;\u5e73\u5747\u503c\u70ba\uff1a{avg}&#8221;)<br \/>\nexcept ValueError as e:<br \/>\n    print(e)<br \/>\n&#8220;`<\/p>\n<p>\u5728\u6b64\u7bc4\u4f8b\u4e2d\uff0c\u51fd\u5f0f `calculate_average` \u63a5\u53d7\u4e00\u500b\u6574\u6578\u5217\u8868\u4e26\u56de\u50b3\u5176\u5e73\u5747\u503c\u3002\u5982\u679c\u5217\u8868\u70ba\u7a7a\uff0c\u5247\u6703\u5f15\u767c\u932f\u8aa4\u3002\u9019\u6a23\u4e0d\u50c5\u63d0\u9ad8\u4e86\u7a0b\u5f0f\u78bc\u7684\u53ef\u8b80\u6027\uff0c\u9084\u5f37\u5316\u4e86\u932f\u8aa4\u8655\u7406\u7684\u80fd\u529b\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528\uff1aPython \u7684\u5176\u4ed6\u578b\u614b\u8a3b\u89e3\u7279\u6027<\/h2>\n<p>\u9664\u4e86\u57fa\u672c\u7684\u53c3\u6578\u548c\u56de\u50b3\u503c\u8a3b\u89e3\uff0cPython 3.9 \u4ee5\u4e0a\u7248\u672c\u9084\u5f15\u5165\u4e86\u66f4\u9748\u6d3b\u7684\u578b\u614b\u8a3b\u89e3\u8a9e\u6cd5\uff0c\u4f8b\u5982\u4f7f\u7528 `list[int]` \u4f86\u8868\u793a\u6574\u6578\u5217\u8868\uff0c\u9019\u4e9b\u7279\u6027\u9032\u4e00\u6b65\u589e\u5f37\u4e86\u578b\u614b\u8a3b\u89e3\u7684\u8868\u9054\u80fd\u529b\u3002\u66f4\u591a\u8a73\u7d30\u8cc7\u8a0a\u53ef\u4ee5\u53c3\u8003 [Python \u5b98\u65b9\u6587\u6a94](https:\/\/docs.python.org\/3\/library\/typing.html)\u3002<\/p>\n<h2>\u7d50\u8ad6<\/h2>\n<p>\u7e3d\u800c\u8a00\u4e4b\uff0cPython \u7684 Annotations \u529f\u80fd\u5c0d\u65bc\u63d0\u9ad8\u7a0b\u5f0f\u78bc\u7684\u53ef\u8b80\u6027\u548c\u975c\u614b\u6aa2\u67e5\u7684\u80fd\u529b\u975e\u5e38\u6709\u5e6b\u52a9\u3002\u96d6\u7136\u6709\u4e9b\u9650\u5236\uff0c\u4f46\u5b83\u5011\u7684\u512a\u9ede\u4f7f\u5176\u6210\u70ba Python \u958b\u767c\u4e2d\u4e00\u500b\u4e0d\u53ef\u6216\u7f3a\u7684\u5de5\u5177\u3002<\/p>\n<h2>\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff08Q&#038;A\uff09<\/h2>\n<p>**Q1: Python Annotations \u53ef\u4ee5\u7528\u65bc\u6240\u6709\u51fd\u5f0f\u55ce\uff1f**<br \/>\nA1: \u662f\u7684\uff0cAnnotations \u53ef\u4ee5\u7528\u65bc\u4efb\u4f55\u51fd\u5f0f\u7684\u53c3\u6578\u548c\u56de\u50b3\u503c\uff0c\u4f46\u7121\u6cd5\u7528\u65bc\u8b8a\u6578\u672c\u8eab\u3002<\/p>\n<p>**Q2: \u5982\u4f55\u6aa2\u67e5\u6211\u7684 Python \u7a0b\u5f0f\u78bc\u4e2d\u662f\u5426\u5b58\u5728\u578b\u614b\u932f\u8aa4\uff1f**<br \/>\nA2: \u60a8\u53ef\u4ee5\u4f7f\u7528\u975c\u614b\u578b\u614b\u6aa2\u67e5\u5de5\u5177\uff0c\u5982 `mypy`\uff0c\u4f86\u6aa2\u67e5\u60a8\u7684\u7a0b\u5f0f\u78bc\u4e2d\u7684\u578b\u614b\u932f\u8aa4\u3002<\/p>\n<p>**Q3: Annotations \u662f\u5426\u6703\u5f71\u97ff\u7a0b\u5f0f\u7684\u57f7\u884c\u6548\u80fd\uff1f**<br \/>\nA3: \u4e0d\u6703\uff0cAnnotations \u53ea\u662f\u4e00\u7a2e\u8a3b\u89e3\uff0c\u4e0d\u6703\u5c0d\u7a0b\u5f0f\u7684\u57f7\u884c\u6548\u80fd\u7522\u751f\u5f71\u97ff\u3002<\/p>\n<p>&#8212;<\/p>\n<p>\u5e0c\u671b\u9019\u4e9b\u512a\u5316\u80fd\u5920\u5e6b\u52a9\u60a8\u7684\u6587\u7ae0\u66f4\u597d\u5730\u5438\u5f15\u8b80\u8005\uff0c\u540c\u6642\u63d0\u5347\u5176\u5728\u641c\u5c0b\u5f15\u64ce\u4e2d\u7684\u6392\u540d\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python\u4e2d\u7684Annotations\u662f\u4e00\u7a2e\u53ef\u4ee5\u63d0\u4f9b\u66f4\u591a\u8cc7\u8a0a\u7684\u8a9e\u6cd5\uff0c\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u5bb9\u6613\u5730\u64b0\u5beb\u7a0b\u5f0f\u78bc\uff0c\u4e26\u63d0\u4f9b\u66f4\u591a\u7684\u53ef\u8b80\u6027\u3002\u672c\u6587\u5c07\u4ecb\u7d39Python\u4e2d\u7684Annotations\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u7c21\u55ae\u6613\u61c2\u7684\u7bc4\u4f8b\uff0c\u8b93\u958b\u767c\u8005\u53ef\u4ee5\u66f4\u5bb9\u6613\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-3332","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-RK","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3332","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=3332"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3332\/revisions"}],"predecessor-version":[{"id":3333,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3332\/revisions\/3333"}],"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=3332"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3332"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3332"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}