{"id":2948,"date":"2025-06-03T16:38:06","date_gmt":"2025-06-04T09:20:05","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=2948"},"modified":"2025-06-04T16:38:06","modified_gmt":"2025-06-04T09:20:05","slug":"%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84getattr%e5%87%bd%e6%95%b8","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\/","title":{"rendered":"\u6df1\u5165\u4e86\u89e3 Python \u7684 `getattr()` \u51fd\u6578\uff1a2025 \u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, getattr()\"><\/p>\n<p>Python \u662f\u4e00\u7a2e\u975e\u5e38\u6d41\u884c\u7684\u7de8\u7a0b\u8a9e\u8a00\uff0c\u5ee3\u6cdb\u7528\u65bc\u5404\u7a2e\u61c9\u7528\u958b\u767c\u3002\u5b83\u63d0\u4f9b\u4e86\u8a31\u591a\u5f37\u5927\u7684\u529f\u80fd\uff0c\u5176\u4e2d\u4e4b\u4e00\u5c31\u662f `getattr()` \u51fd\u6578\u3002\u9019\u500b\u51fd\u6578\u53ef\u4ee5\u7528\u4f86\u8f15\u9b06\u5730\u8a2a\u554f\u5c0d\u8c61\u7684\u7279\u5b9a\u5c6c\u6027\uff0c\u5c0d\u65bc\u52d5\u614b\u5c6c\u6027\u8a2a\u554f\u7279\u5225\u6709\u5e6b\u52a9\u3002<\/p>\n<h2>\u4ec0\u9ebc\u662f `getattr()` \u51fd\u6578\uff1f<\/h2>\n<p>`getattr()` \u51fd\u6578\u662f Python \u4e2d\u7684\u4e00\u500b\u5167\u7f6e\u51fd\u6578\uff0c\u7528\u65bc\u8fd4\u56de\u5c0d\u8c61\u7684\u7279\u5b9a\u5c6c\u6027\u3002\u9019\u500b\u51fd\u6578\u63a5\u53d7\u4e09\u500b\u53c3\u6578\uff1a<br \/>\n&#8211; **\u5c0d\u8c61**\uff1a\u8981\u67e5\u8a62\u7684\u5c0d\u8c61<br \/>\n&#8211; **\u5c6c\u6027\u540d\u7a31**\uff1a\u8981\u8a2a\u554f\u7684\u5c6c\u6027\u540d\u7a31\uff08\u5b57\u7b26\u4e32\u5f62\u5f0f\uff09<br \/>\n&#8211; **\u9ed8\u8a8d\u503c**\uff08\u53ef\u9078\uff09\uff1a\u5982\u679c\u6307\u5b9a\u7684\u5c6c\u6027\u4e0d\u5b58\u5728\uff0c\u5247\u8fd4\u56de\u7684\u9ed8\u8a8d\u503c<\/p>\n<p>\u5982\u679c\u5c0d\u8c61\u5177\u6709\u6307\u5b9a\u7684\u5c6c\u6027\uff0c\u5247\u8fd4\u56de\u8a72\u5c6c\u6027\u7684\u503c\uff1b\u5426\u5247\uff0c\u5c07\u8fd4\u56de\u9ed8\u8a8d\u503c\uff08\u5982\u679c\u6307\u5b9a\uff09\uff0c\u6216\u8005\u62cb\u51fa `AttributeError` \u7570\u5e38\u3002<\/p>\n<h2>`getattr()` \u51fd\u6578\u7684\u793a\u4f8b<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u4f7f\u7528 `getattr()` \u51fd\u6578\u7684\u5be6\u4f5c\u7bc4\u4f8b\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;, 25)<\/p>\n<p># \u7372\u53d6 name \u5c6c\u6027<br \/>\nname = getattr(person, &#8216;name&#8217;)<br \/>\nprint(name)  # \u8f38\u51fa: John<\/p>\n<p># \u7372\u53d6 age \u5c6c\u6027<br \/>\nage = getattr(person, &#8216;age&#8217;)<br \/>\nprint(age)  # \u8f38\u51fa: 25<\/p>\n<p># \u7372\u53d6 gender \u5c6c\u6027\uff08\u8a72\u5c6c\u6027\u4e0d\u5b58\u5728\uff09<br \/>\ngender = getattr(person, &#8216;gender&#8217;, &#8216;Male&#8217;)<br \/>\nprint(gender)  # \u8f38\u51fa: Male<br \/>\n&#8220;`<\/p>\n<p>\u5728\u9019\u500b\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u5b9a\u7fa9\u4e86\u4e00\u500b `Person` \u985e\uff0c\u5176\u4e2d\u5305\u542b `name` \u548c `age` \u5c6c\u6027\u3002\u7136\u5f8c\uff0c\u6211\u5011\u5275\u5efa\u4e86\u4e00\u500b `Person` \u5c0d\u8c61\uff0c\u4e26\u4f7f\u7528 `getattr()` \u51fd\u6578\u4f86\u8a2a\u554f\u5c0d\u8c61\u7684 `name` \u548c `age` \u5c6c\u6027\u3002\u7576\u6211\u5011\u5617\u8a66\u8a2a\u554f\u4e0d\u5b58\u5728\u7684 `gender` \u5c6c\u6027\u6642\uff0c`getattr()` \u8fd4\u56de\u4e86\u6211\u5011\u6307\u5b9a\u7684\u9ed8\u8a8d\u503c \u201cMale\u201d\u3002<\/p>\n<h2>\u5e38\u898b\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5728\u4f7f\u7528 `getattr()` \u51fd\u6578\u6642\uff0c\u53ef\u80fd\u6703\u9047\u5230\u4ee5\u4e0b\u5e38\u898b\u932f\u8aa4\uff1a<\/p>\n<p>1. **AttributeError**\uff1a\u7576\u4f60\u5617\u8a66\u8a2a\u554f\u4e00\u500b\u4e0d\u5b58\u5728\u7684\u5c6c\u6027\u4e14\u6c92\u6709\u63d0\u4f9b\u9ed8\u8a8d\u503c\u6642\uff0c\u6703\u5f15\u767c\u6b64\u932f\u8aa4\u3002\u78ba\u4fdd\u4f60\u63d0\u4f9b\u4e86\u6b63\u78ba\u7684\u5c6c\u6027\u540d\u7a31\uff0c\u6216\u8005\u4f7f\u7528\u9ed8\u8a8d\u503c\u4f86\u907f\u514d\u6b64\u932f\u8aa4\u3002<\/p>\n<p>2. **\u985e\u578b\u932f\u8aa4**\uff1a\u5982\u679c\u4f60\u50b3\u905e\u7684\u5c0d\u8c61\u4e0d\u662f Python \u5c0d\u8c61\uff0c\u5247 `getattr()` \u53ef\u80fd\u7121\u6cd5\u6b63\u5e38\u5de5\u4f5c\u3002\u8acb\u78ba\u4fdd\u6240\u63d0\u4f9b\u7684\u5c0d\u8c61\u662f\u6709\u6548\u7684 Python \u5c0d\u8c61\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>`getattr()` \u51fd\u6578\u5728\u8a31\u591a\u60c5\u6cc1\u4e0b\u90fd\u80fd\u6d3e\u4e0a\u7528\u5834\uff0c\u4f8b\u5982\uff1a<br \/>\n&#8211; \u5728\u958b\u767c API \u6642\uff0c\u6839\u64da\u7528\u6236\u8acb\u6c42\u52d5\u614b\u8fd4\u56de\u4e0d\u540c\u7684\u5c0d\u8c61\u5c6c\u6027\u3002<br \/>\n&#8211; \u5728\u5be6\u73fe ORM\uff08\u7269\u4ef6\u95dc\u806f\u6620\u5c04\uff09\u6642\uff0c\u6839\u64da\u67e5\u8a62\u5c0d\u8c61\u7684\u5c6c\u6027\u52d5\u614b\u751f\u6210 SQL \u67e5\u8a62\u3002<\/p>\n<p>\u4e86\u89e3\u5982\u4f55\u4f7f\u7528 `getattr()` \u51fd\u6578\u5c07\u6709\u52a9\u65bc\u63d0\u5347\u4f60\u7684 Python \u6280\u80fd\uff0c\u8b93\u4f60\u80fd\u5920\u7de8\u5beb\u66f4\u9748\u6d3b\u548c\u53ef\u7dad\u8b77\u7684\u4ee3\u78bc\u3002<\/p>\n<p>\u5982\u9700\u9032\u4e00\u6b65\u4e86\u89e3 Python \u7684\u66f4\u591a\u529f\u80fd\uff0c\u8acb\u53c3\u8003 [\u9019\u88e1\u7684\u6559\u5b78\u6587\u7ae0](https:\/\/vocus.cc\/article\/603c4fd2fd89780006a2e3f5)\u3002<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>`getattr()` \u51fd\u6578\u662f Python \u4e2d\u4e00\u500b\u975e\u5e38\u5be6\u7528\u7684\u5167\u7f6e\u51fd\u6578\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u7a2e\u7c21\u55ae\u800c\u6709\u6548\u7684\u65b9\u6cd5\u4f86\u6aa2\u67e5\u5c0d\u8c61\u662f\u5426\u5177\u6709\u7279\u5b9a\u7684\u5c6c\u6027\uff0c\u4e26\u5728\u9700\u8981\u6642\u8fd4\u56de\u9ed8\u8a8d\u503c\u3002\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u8f15\u9b06\u5730\u8a2a\u554f\u5c0d\u8c61\u7684\u5c6c\u6027\uff0c\u63d0\u5347\u4ee3\u78bc\u7684\u9748\u6d3b\u6027\u548c\u53ef\u8b80\u6027\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<p>**Q1: `getattr()` \u53ef\u4ee5\u7528\u65bc\u54ea\u4e9b\u985e\u578b\u7684\u5c0d\u8c61\uff1f**<br \/>\nA1: `getattr()` \u53ef\u4ee5\u7528\u65bc\u4efb\u4f55 Python \u5c0d\u8c61\uff0c\u5305\u62ec\u985e\u3001\u5be6\u4f8b\u3001\u6a21\u7d44\u7b49\u3002<\/p>\n<p>**Q2: \u5982\u679c\u6211\u4e0d\u63d0\u4f9b\u9ed8\u8a8d\u503c\uff0c\u6703\u767c\u751f\u4ec0\u9ebc\uff1f**<br \/>\nA2: \u5982\u679c\u4e0d\u63d0\u4f9b\u9ed8\u8a8d\u503c\uff0c\u4e26\u4e14\u6307\u5b9a\u7684\u5c6c\u6027\u4e0d\u5b58\u5728\uff0c\u5c07\u5f15\u767c `AttributeError` \u7570\u5e38\u3002<\/p>\n<p>**Q3: `getattr()` \u51fd\u6578\u7684\u6027\u80fd\u5982\u4f55\uff1f**<br \/>\nA3: `getattr()` \u7684\u6027\u80fd\u8207\u76f4\u63a5\u8a2a\u554f\u5c6c\u6027\u76f8\u7576\uff0c\u4f46\u5728\u4f7f\u7528\u6642\u9700\u8003\u616e\u5230\u5176\u9748\u6d3b\u6027\u8207\u53ef\u8b80\u6027\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3Python\u4e2d\u7684getattr()\u51fd\u6578\uff0c\u5b83\u53ef\u4ee5\u7528\u65bc\u8fd4\u56de\u5c0d\u8c61\u7684\u7279\u5b9a\u5c6c\u6027\uff0c\u4e26\u63d0\u4f9b\u4e86\u4e00\u7a2e\u7c21\u55ae\u7684\u65b9\u6cd5\u4f86\u8a2a\u554f\u5c0d\u8c61\u7684\u5c6c\u6027\u3002\u672c\u6587\u5c07\u8a73\u7d30\u4ecb\u7d39getattr()\u51fd\u6578\u7684\u7528\u6cd5\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u793a\u4f8b\u4f86\u8aaa\u660e\u5b83\u7684\u529f\u80fd\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-2948","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-Ly","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2948","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=2948"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2948\/revisions"}],"predecessor-version":[{"id":2949,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2948\/revisions\/2949"}],"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=2948"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=2948"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=2948"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}