{"id":3446,"date":"2025-05-31T15:06:30","date_gmt":"2025-06-04T07:44:29","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3446"},"modified":"2025-06-04T15:06:30","modified_gmt":"2025-06-04T07:44:29","slug":"%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84getattr%e5%87%bd%e6%95%b8%e5%8a%9f%e8%83%bd","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84getattr%e5%87%bd%e6%95%b8%e5%8a%9f%e8%83%bd\/","title":{"rendered":"\u6df1\u5165\u4e86\u89e3 Python \u4e2d\u7684 getattr() \u51fd\u6578\uff1a\u529f\u80fd\u3001\u7bc4\u4f8b\u8207\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, getattr(), Python \u6559\u5b78, Python \u51fd\u6578\"><\/p>\n<h1>\u6df1\u5165\u4e86\u89e3 Python \u4e2d\u7684 getattr() \u51fd\u6578<\/h1>\n<p>\u5728 Python \u7a0b\u5f0f\u8a2d\u8a08\u4e2d\uff0c`getattr()` \u51fd\u6578\u662f\u4e00\u500b\u975e\u5e38\u5be6\u7528\u7684\u5de5\u5177\uff0c\u80fd\u5920\u5e6b\u52a9\u6211\u5011\u53d6\u5f97\u7269\u4ef6\u7684\u5c6c\u6027\u503c\u3002\u9019\u500b\u51fd\u6578\u63a5\u53d7\u4e09\u500b\u53c3\u6578\uff1a\u7269\u4ef6\u3001\u5c6c\u6027\u540d\u7a31\uff0c\u4ee5\u53ca\u4e00\u500b\u53ef\u9078\u7684\u9810\u8a2d\u503c\u3002\u7576\u7269\u4ef6\u4e2d\u5b58\u5728\u8a72\u5c6c\u6027\u6642\uff0c`getattr()` \u6703\u8fd4\u56de\u8a72\u5c6c\u6027\u7684\u503c\uff1b\u82e5\u4e0d\u5b58\u5728\uff0c\u5247\u8fd4\u56de\u6307\u5b9a\u7684\u9810\u8a2d\u503c\uff08\u5982\u679c\u6709\u63d0\u4f9b\uff09\uff0c\u5426\u5247\u6703\u62cb\u51fa `AttributeError` \u7570\u5e38\u3002<\/p>\n<h2>\u4f7f\u7528 getattr() \u51fd\u6578\u7684\u7bc4\u4f8b<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u4f7f\u7528 `getattr()` \u51fd\u6578\u7684\u7bc4\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u6709\u6548\u5730\u7372\u53d6\u7269\u4ef6\u7684\u5c6c\u6027\uff1a<\/p>\n<p>&#8220;`python<br \/>\nclass Person:<br \/>\n    def __init__(self, name, age):<br \/>\n        self.name = name<br \/>\n        self.age = age<\/p>\n<p>person = Person(&#8216;John&#8217;, 20)<\/p>\n<p># \u53d6\u5f97 name \u5c6c\u6027<br \/>\nname = getattr(person, &#8216;name&#8217;)<br \/>\nprint(name)  # John<\/p>\n<p># \u53d6\u5f97 age \u5c6c\u6027<br \/>\nage = getattr(person, &#8216;age&#8217;)<br \/>\nprint(age)  # 20<\/p>\n<p># \u53d6\u5f97\u4e0d\u5b58\u5728\u7684\u5c6c\u6027\uff0c\u4e26\u6307\u5b9a\u9810\u8a2d\u503c<br \/>\ngender = getattr(person, &#8216;gender&#8217;, &#8216;Male&#8217;)<br \/>\nprint(gender)  # Male<br \/>\n&#8220;`<\/p>\n<p>\u5728\u9019\u500b\u7bc4\u4f8b\u4e2d\uff0c\u6211\u5011\u5275\u5efa\u4e86\u4e00\u500b `Person` \u7269\u4ef6\uff0c\u4e26\u5229\u7528 `getattr()` \u7372\u53d6\u5b83\u7684 `name` \u548c `age` \u5c6c\u6027\u3002\u6700\u5f8c\uff0c\u7576\u6211\u5011\u8a66\u5716\u53d6\u5f97\u4e0d\u5b58\u5728\u7684 `gender` \u5c6c\u6027\u6642\uff0c`getattr()` \u8fd4\u56de\u4e86\u6211\u5011\u6307\u5b9a\u7684\u9810\u8a2d\u503c `Male`\uff0c\u9019\u6a23\u7684\u8a2d\u8a08\u53ef\u4ee5\u907f\u514d\u6f5b\u5728\u7684\u932f\u8aa4\u3002<\/p>\n<h2>getattr() \u51fd\u6578\u7684\u512a\u9ede<\/h2>\n<p>\u4f7f\u7528 `getattr()` \u51fd\u6578\u7684\u4e3b\u8981\u512a\u52e2\u5728\u65bc\u5b83\u80fd\u5920\u7c21\u5316\u5c6c\u6027\u8a2a\u554f\u7684\u904e\u7a0b\u3002\u5728\u8a2a\u554f\u7269\u4ef6\u5c6c\u6027\u6642\uff0c\u6211\u5011\u4e0d\u5fc5\u64d4\u5fc3\u5c6c\u6027\u662f\u5426\u5b58\u5728\uff0c\u9019\u6a23\u53ef\u4ee5\u964d\u4f4e\u7a0b\u5f0f\u51fa\u932f\u7684\u98a8\u96aa\uff0c\u7279\u5225\u662f\u5728\u8655\u7406\u5927\u578b\u7269\u4ef6\u6216\u52d5\u614b\u751f\u6210\u7684\u5c6c\u6027\u6642\u3002<\/p>\n<h2>\u9032\u968e\u61c9\u7528\u8207\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5728\u5be6\u969b\u958b\u767c\u4e2d\uff0c`getattr()` \u4e5f\u53ef\u4ee5\u61c9\u7528\u65bc\u8a31\u591a\u9032\u968e\u5834\u666f\uff0c\u4f8b\u5982\u7576\u4f60\u4e0d\u77e5\u9053\u7269\u4ef6\u7684\u5c6c\u6027\u540d\u7a31\u6642\uff0c\u53ef\u4ee5\u5229\u7528 `getattr()` \u52d5\u614b\u5730\u7372\u53d6\u5c6c\u6027\u3002\u6b64\u5916\uff0c\u7576\u4f60\u9047\u5230 `AttributeError` \u6642\uff0c\u53ef\u4ee5\u4f7f\u7528 `try-except` \u69cb\u9020\u4f86\u6355\u7372\u4e26\u8655\u7406\u7570\u5e38\uff1a<\/p>\n<p>&#8220;`python<br \/>\ntry:<br \/>\n    value = getattr(person, &#8216;non_existent_attribute&#8217;)<br \/>\nexcept AttributeError:<br \/>\n    print(&#8220;Attribute does not exist.&#8221;)<br \/>\n&#8220;`<\/p>\n<p>\u9019\u6a23\u7684\u8655\u7406\u65b9\u5f0f\u53ef\u4ee5\u589e\u52a0\u7a0b\u5f0f\u7684\u7a69\u5b9a\u6027\u548c\u53ef\u8b80\u6027\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u9664\u4e86\u57fa\u672c\u7684\u5c6c\u6027\u8a2a\u554f\u5916\uff0c`getattr()` \u9084\u53ef\u4ee5\u8207 `setattr()` \u4e00\u8d77\u4f7f\u7528\uff0c\u5be6\u73fe\u66f4\u52a0\u9748\u6d3b\u7684\u7269\u4ef6\u5c6c\u6027\u7ba1\u7406\u3002\u901a\u904e\u9019\u4e9b\u51fd\u6578\uff0c\u4f60\u53ef\u4ee5\u5728\u904b\u884c\u6642\u52d5\u614b\u5730\u8a2d\u7f6e\u548c\u7372\u53d6\u7269\u4ef6\u5c6c\u6027\uff0c\u9019\u5728\u67d0\u4e9b\u61c9\u7528\u4e2d\u975e\u5e38\u6709\u7528\u3002<\/p>\n<p>\u60f3\u4e86\u89e3\u66f4\u591a\u95dc\u65bc Python \u7684\u77e5\u8b58\u8207\u6280\u5de7\uff0c\u5efa\u8b70\u53c3\u8003 [\u9019\u88e1\u7684\u6559\u5b78](https:\/\/vocus.cc\/article\/605c1c1f87917d001c3a2b7b) \u7372\u53d6\u66f4\u591a\u6df1\u5165\u5167\u5bb9\u3002<\/p>\n<h2>\u7d50\u8ad6<\/h2>\n<p>\u7e3d\u7d50\u4f86\u8aaa\uff0cPython \u7684 `getattr()` \u51fd\u6578\u662f\u4e00\u500b\u5f37\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u5e6b\u52a9\u6211\u5011\u5728\u4e0d\u78ba\u5b9a\u5c6c\u6027\u5b58\u5728\u8207\u5426\u7684\u60c5\u6cc1\u4e0b\u5b89\u5168\u5730\u7372\u53d6\u7269\u4ef6\u7684\u5c6c\u6027\u503c\u3002\u900f\u904e\u7bc4\u4f8b\u8207\u61c9\u7528\uff0c\u6211\u5011\u53ef\u4ee5\u770b\u5230\u5b83\u5728\u5be6\u969b\u958b\u767c\u4e2d\u7684\u91cd\u8981\u6027\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<p>**Q1: \u5982\u4f55\u8655\u7406 `getattr()` \u7684 `AttributeError`\uff1f**<br \/>\nA1: \u53ef\u4ee5\u4f7f\u7528 `try-except` \u7d50\u69cb\u6355\u7372\u6b64\u7570\u5e38\uff0c\u78ba\u4fdd\u7a0b\u5f0f\u4e0d\u6703\u56e0\u70ba\u8a2a\u554f\u4e0d\u5b58\u5728\u7684\u5c6c\u6027\u800c\u5d29\u6f70\u3002<\/p>\n<p>**Q2: `getattr()` \u53ef\u4ee5\u7528\u65bc\u54ea\u4e9b\u985e\u578b\u7684\u7269\u4ef6\uff1f**<br \/>\nA2: `getattr()` \u53ef\u4ee5\u7528\u65bc\u6240\u6709 Python \u7269\u4ef6\uff0c\u5305\u62ec\u81ea\u5b9a\u7fa9\u985e\u5225\u7684\u5be6\u4f8b\u3001\u5167\u5efa\u985e\u5225\u7b49\u3002<\/p>\n<p>**Q3: `getattr()` \u7684\u9810\u8a2d\u503c\u53c3\u6578\u6709\u4ec0\u9ebc\u4f5c\u7528\uff1f**<br \/>\nA3: \u9810\u8a2d\u503c\u53c3\u6578\u53ef\u4ee5\u7528\u4f86\u6307\u5b9a\u7576\u7269\u4ef6\u4e2d\u4e0d\u5b58\u5728\u8a72\u5c6c\u6027\u6642\u8fd4\u56de\u7684\u503c\uff0c\u9019\u6a23\u53ef\u4ee5\u9632\u6b62\u62cb\u51fa\u7570\u5e38\u4e26\u63d0\u4f9b\u4e00\u500b\u5f8c\u5099\u9078\u9805\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>getattr()\u51fd\u6578\u662fPython\u4e2d\u7684\u4e00\u500b\u5167\u5efa\u51fd\u6578\uff0c\u53ef\u4ee5\u7528\u4f86\u7372\u53d6\u5c0d\u8c61\u7684\u5c6c\u6027\u503c\uff0c\u53ef\u4ee5\u7528\u4f86\u66ff\u4ee3\u7e41\u7463\u7684if-else\u8a9e\u53e5\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u53ef\u8b80\u6027\u548c\u53ef\u7dad\u8b77\u6027\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-3446","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-TA","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3446","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=3446"}],"version-history":[{"count":2,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3446\/revisions"}],"predecessor-version":[{"id":12830,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3446\/revisions\/12830"}],"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=3446"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3446"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3446"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}