{"id":3138,"date":"2025-06-03T16:43:54","date_gmt":"2025-06-04T08:49:53","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3138"},"modified":"2025-06-04T16:43:54","modified_gmt":"2025-06-04T08:49:53","slug":"%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84getattribute%e5%87%bd%e6%95%b8-2","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84getattribute%e5%87%bd%e6%95%b8-2\/","title":{"rendered":"\u6df1\u5165\u4e86\u89e3 Python \u4e2d\u7684 `getattr()` \u51fd\u6578\uff1a\u7528\u6cd5\u3001\u7bc4\u4f8b\u8207\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, getattr()\"><\/p>\n<h1>Python \u4e2d\u7684 `getattr()` \u65b9\u6cd5<\/h1>\n<p>\u5728 Python \u4e2d\uff0c`getattr()` \u65b9\u6cd5\u662f\u4e00\u500b\u975e\u5e38\u5be6\u7528\u7684\u51fd\u5f0f\uff0c\u5b83\u5141\u8a31\u4f60\u5f9e\u7269\u4ef6\u4e2d\u52d5\u614b\u5730\u53d6\u5f97\u5c6c\u6027\u503c\uff0c\u800c\u4e0d\u5fc5\u786c\u7de8\u78bc\u7279\u5b9a\u7684\u5c6c\u6027\u540d\u7a31\u3002\u9019\u4e00\u65b9\u6cd5\u5728\u8655\u7406\u4e0d\u78ba\u5b9a\u6027\u6216\u52d5\u614b\u5c6c\u6027\u6642\u5c24\u5176\u6709\u7528\u3002<\/p>\n<p>`getattr()` \u65b9\u6cd5\u7684\u8a9e\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush: python\">\ngetattr(object, attribute[, default])\n<\/pre>\n<p>&#8211; `object`\uff1a\u4f60\u60f3\u8981\u53d6\u5f97\u5c6c\u6027\u503c\u7684\u7269\u4ef6\u3002<br \/>\n&#8211; `attribute`\uff1a\u4f60\u60f3\u8981\u53d6\u5f97\u7684\u5c6c\u6027\u540d\u7a31\uff08\u4ee5\u5b57\u4e32\u5f62\u5f0f\u50b3\u5165\uff09\u3002<br \/>\n&#8211; `default`\uff1a\u53ef\u9078\u53c3\u6578\uff0c\u7576\u6307\u5b9a\u7684\u5c6c\u6027\u4e0d\u5b58\u5728\u6642\uff0c\u8fd4\u56de\u7684\u9810\u8a2d\u503c\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u7bc4\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528 `getattr()` \u65b9\u6cd5\uff1a<\/p>\n<pre class=\"brush: python\">\nclass Person:\n    def __init__(self, name, age):\n        self.name = name\n        self.age = age\n\nperson = Person(\"John\", 30)\n\n# \u4f7f\u7528 getattr() \u65b9\u6cd5\u53d6\u5f97 name \u548c age \u5c6c\u6027\u7684\u503c\nname = getattr(person, \"name\")\nage = getattr(person, \"age\")\n\nprint(\"Name:\", name)\nprint(\"Age:\", age)\n<\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u7bc4\u4f8b\u4e2d\uff0c\u6211\u5011\u5b9a\u7fa9\u4e86\u4e00\u500b `Person` \u985e\u5225\u4e26\u5efa\u7acb\u4e86\u4e00\u500b `Person` \u7269\u4ef6\u3002\u7136\u5f8c\uff0c\u6211\u5011\u4f7f\u7528 `getattr()` \u65b9\u6cd5\u53d6\u5f97 `name` \u548c `age` \u5c6c\u6027\u7684\u503c\uff0c\u4e26\u5c07\u5b83\u5011\u5370\u51fa\u4f86\u3002<\/p>\n<p>\u6b64\u5916\uff0c`getattr()` \u65b9\u6cd5\u4e5f\u53ef\u4ee5\u7528\u4f86\u53d6\u5f97\u7269\u4ef6\u7684\u65b9\u6cd5\uff1a<\/p>\n<pre class=\"brush: python\">\nclass Person:\n    def __init__(self, name, age):\n        self.name = name\n        self.age = age\n\n    def say_hello(self):\n        print(\"Hello, my name is\", self.name)\n\nperson = Person(\"John\", 30)\n\n# \u4f7f\u7528 getattr() \u65b9\u6cd5\u53d6\u5f97 say_hello \u65b9\u6cd5\nsay_hello = getattr(person, \"say_hello\")\n\n# \u547c\u53eb say_hello \u65b9\u6cd5\nsay_hello()\n<\/pre>\n<p>\u5728\u9019\u500b\u7bc4\u4f8b\u4e2d\uff0c\u6211\u5011\u4f7f\u7528 `getattr()` \u65b9\u6cd5\u53d6\u5f97 `say_hello` \u65b9\u6cd5\u4e26\u547c\u53eb\u5b83\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4ee5\u52d5\u614b\u65b9\u5f0f\u5b58\u53d6\u7269\u4ef6\u7684\u65b9\u6cd5\u3002<\/p>\n<h2>`getattr()` \u65b9\u6cd5\u7684\u512a\u9ede<\/h2>\n<p>`getattr()` \u65b9\u6cd5\u7684\u4e3b\u8981\u512a\u9ede\u5728\u65bc\u5176\u9748\u6d3b\u6027\uff0c\u80fd\u5920\u8b93\u4f60\u5728\u4e0d\u77e5\u9053\u7269\u4ef6\u78ba\u5207\u5c6c\u6027\u540d\u7a31\u7684\u60c5\u6cc1\u4e0b\uff0c\u4ecd\u80fd\u6210\u529f\u5b58\u53d6\u5c6c\u6027\u3002\u9019\u5c0d\u65bc\u8655\u7406\u5927\u578b\u8cc7\u6599\u7d50\u69cb\u6216\u5f9e\u5916\u90e8\u8cc7\u6599\u4f86\u6e90\u7372\u53d6\u8cc7\u6599\u6642\uff0c\u7279\u5225\u6709\u7528\u3002\u4f8b\u5982\uff0c\u7576\u5c6c\u6027\u540d\u7a31\u4f86\u81ea\u7528\u6236\u8f38\u5165\u6216 API \u8fd4\u56de\u7684 JSON \u8cc7\u6599\u6642\uff0c`getattr()` \u80fd\u5920\u6709\u6548\u907f\u514d\u5c6c\u6027\u62fc\u5beb\u932f\u8aa4\u7684\u554f\u984c\u3002<\/p>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u4f7f\u7528 `getattr()` \u6642\uff0c\u5982\u679c\u4f60\u4e0d\u78ba\u5b9a\u5c6c\u6027\u662f\u5426\u5b58\u5728\uff0c\u53ef\u4ee5\u5229\u7528 `default` \u53c3\u6578\u907f\u514d `AttributeError` \u932f\u8aa4\u3002\u4f8b\u5982\uff1a<\/p>\n<pre class=\"brush: python\">\n# \u5617\u8a66\u7372\u53d6\u4e00\u500b\u4e0d\u5b58\u5728\u7684\u5c6c\u6027\nnon_existent_attr = getattr(person, \"non_existent_attr\", \"\u5c6c\u6027\u4e0d\u5b58\u5728\")\nprint(non_existent_attr)  # \u8f38\u51fa\uff1a\u5c6c\u6027\u4e0d\u5b58\u5728\n<\/pre>\n<p>\u9019\u6a23\u53ef\u4ee5\u4fdd\u8b49\u5728\u5c6c\u6027\u4e0d\u5b58\u5728\u7684\u60c5\u6cc1\u4e0b\uff0c\u4f60\u7684\u7a0b\u5f0f\u4e0d\u6703\u5d29\u6f70\uff0c\u800c\u662f\u8fd4\u56de\u4e00\u500b\u9810\u8a2d\u503c\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>`getattr()` \u65b9\u6cd5\u5728\u8a31\u591a\u60c5\u5883\u4e0b\u90fd\u975e\u5e38\u6709\u7528\uff0c\u4f8b\u5982\uff1a<\/p>\n<p>&#8211; \u52d5\u614b\u751f\u6210\u5c6c\u6027\u540d\u7a31\u6642<br \/>\n&#8211; \u5728\u4f7f\u7528 ORM\uff08\u7269\u4ef6\u95dc\u806f\u6620\u5c04\uff09\u6642\u81ea\u52d5\u5b58\u53d6\u8cc7\u6599\u5eab\u6b04\u4f4d<br \/>\n&#8211; \u5728\u9032\u884c\u53cd\u5c04\uff08Reflection\uff09\u6216\u5143\u7de8\u7a0b\uff08Metaprogramming\uff09\u6642<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u4ecb\u7d39\u4e86 Python \u4e2d\u7684 `getattr()` \u65b9\u6cd5\u3002\u9019\u500b\u65b9\u6cd5\u4e0d\u50c5\u80fd\u5920\u8b93\u4f60\u5f9e\u7269\u4ef6\u4e2d\u53d6\u5f97\u5c6c\u6027\u503c\uff0c\u9084\u80fd\u5920\u5728\u5c6c\u6027\u4e0d\u5b58\u5728\u6642\u63d0\u4f9b\u9810\u8a2d\u503c\uff0c\u5f9e\u800c\u589e\u5f37\u4f60\u7684\u7a0b\u5f0f\u7684\u5065\u58ef\u6027\u548c\u9748\u6d3b\u6027\u3002<\/p>\n<p>\u5982\u9700\u66f4\u6df1\u5165\u7684 Python \u6559\u5b78\uff0c\u6b61\u8fce\u67e5\u770b [Vocus \u7684 Python \u6559\u5b78\u6587\u7ae0](https:\/\/vocus.cc) \u6216 [Miner \u7684 Python \u8cc7\u6e90](https:\/\/miner.tw)\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<p>**Q1: `getattr()` \u8207 `getattribute()` \u6709\u4ec0\u9ebc\u4e0d\u540c\uff1f**<br \/>\nA1: `getattr()` \u7528\u65bc\u5f9e\u7269\u4ef6\u4e2d\u52d5\u614b\u7372\u53d6\u5c6c\u6027\uff0c\u4e26\u53ef\u8a2d\u7f6e\u9810\u8a2d\u503c\uff1b\u800c `getattribute()` \u662f\u4e00\u500b\u66f4\u5e95\u5c64\u7684\u65b9\u6cd5\uff0c\u4e3b\u8981\u5728\u7269\u4ef6\u7684\u5c6c\u6027\u7372\u53d6\u904e\u7a0b\u4e2d\u4f7f\u7528\u3002<\/p>\n<p>**Q2: `getattr()` \u53ef\u4ee5\u7528\u65bc\u81ea\u5b9a\u7fa9\u985e\u5225\u55ce\uff1f**<br \/>\nA2: \u662f\u7684\uff0c`getattr()` \u5b8c\u5168\u53ef\u4ee5\u7528\u65bc\u81ea\u5b9a\u7fa9\u985e\u5225\uff0c\u4e26\u4e14\u652f\u6301\u81ea\u5b9a\u7fa9\u5c6c\u6027\u548c\u65b9\u6cd5\u3002<\/p>\n<p>**Q3: \u7576\u4f7f\u7528 `getattr()` \u6642\uff0c\u5982\u4f55\u8655\u7406\u4e0d\u5b58\u5728\u7684\u5c6c\u6027\uff1f**<br \/>\nA3: \u4f60\u53ef\u4ee5\u4f7f\u7528 `getattr()` \u7684\u7b2c\u4e09\u500b\u53c3\u6578\u4f86\u8a2d\u5b9a\u9810\u8a2d\u503c\uff0c\u7576\u5c6c\u6027\u4e0d\u5b58\u5728\u6642\uff0c\u8fd4\u56de\u8a72\u9810\u8a2d\u503c\uff0c\u907f\u514d\u7a0b\u5f0f\u5d29\u6f70\u3002<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>getattribute()\u51fd\u6578\u662fPython\u4e2d\u7684\u4e00\u500b\u91cd\u8981\u51fd\u6578\uff0c\u5b83\u53ef\u4ee5\u8b93\u4f60\u8a2a\u554f\u7269\u4ef6\u7684\u5c6c\u6027\uff0c\u4e26\u63d0\u4f9b\u4e00\u500b\u7c21\u55ae\u7684\u65b9\u6cd5\u4f86\u8a2a\u554f\u7269\u4ef6\u7684\u5c6c\u6027\u3002\u672c\u6587\u5c07\u4ecb\u7d39getattribute()\u51fd\u6578\u7684\u4f7f\u7528\u65b9\u6cd5\uff0c\u4ee5\u53ca\u5b83\u5982\u4f55\u5e6b\u52a9\u4f60\u66f4\u597d\u5730\u7ba1\u7406\u7269\u4ef6\u7684\u5c6c\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-3138","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-OC","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3138","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=3138"}],"version-history":[{"count":3,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3138\/revisions"}],"predecessor-version":[{"id":12928,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3138\/revisions\/12928"}],"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=3138"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3138"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3138"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}