{"id":3006,"date":"2025-06-01T17:01:08","date_gmt":"2025-06-04T09:11:07","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3006"},"modified":"2025-06-04T17:01:08","modified_gmt":"2025-06-04T09:11:07","slug":"%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84hasattr%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%84hasattr%e5%87%bd%e6%95%b8-2\/","title":{"rendered":"\u3010\u6df1\u5165\u4e86\u89e3 Python \u7684 hasattr() \u51fd\u5f0f\uff1a\u7528\u6cd5\u3001\u7bc4\u4f8b\u8207\u6700\u4f73\u5be6\u8e10\u3011"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, hasattr, Python \u51fd\u5f0f, \u7a0b\u5f0f\u8a2d\u8a08\u6559\u5b78\"><\/p>\n<h1>\u6df1\u5165\u4e86\u89e3 Python \u7684 hasattr() \u51fd\u5f0f<\/h1>\n<p>\u5728 Python \u4e2d\uff0c`hasattr()` \u51fd\u5f0f\u662f\u4e00\u500b\u975e\u5e38\u6709\u7528\u7684\u5167\u5efa\u51fd\u5f0f\uff0c\u7528\u65bc\u6aa2\u67e5\u7269\u4ef6\u662f\u5426\u64c1\u6709\u7279\u5b9a\u7684\u5c6c\u6027\u3002\u9019\u500b\u51fd\u5f0f\u4e0d\u50c5\u80fd\u5920\u5e6b\u52a9\u6211\u5011\u907f\u514d\u7a0b\u5f0f\u51fa\u73fe\u932f\u8aa4\uff0c\u9084\u80fd\u589e\u5f37\u4ee3\u78bc\u7684\u53ef\u8b80\u6027\u3002\u5728\u9019\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u5011\u5c07\u6df1\u5165\u63a2\u8a0e `hasattr()` \u7684\u7528\u6cd5\uff0c\u63d0\u4f9b\u5be6\u4f5c\u7bc4\u4f8b\uff0c\u4ee5\u53ca\u4e00\u4e9b\u6700\u4f73\u5be6\u8e10\u8207\u932f\u8aa4\u6392\u9664\u5efa\u8b70\u3002<\/p>\n<p>### `hasattr()` \u7684\u8a9e\u6cd5<\/p>\n<p>`hasattr()` \u7684\u8a9e\u6cd5\u5982\u4e0b\uff1a<\/p>\n<p>&#8220;`python<br \/>\nhasattr(object, name)<br \/>\n&#8220;`<\/p>\n<p>&#8211; **object**\uff1a\u8981\u6aa2\u67e5\u7684\u7269\u4ef6\u3002<br \/>\n&#8211; **name**\uff1a\u8981\u6aa2\u67e5\u7684\u5c6c\u6027\u540d\u7a31\uff0c\u5fc5\u9808\u662f\u5b57\u4e32\u683c\u5f0f\u3002<\/p>\n<p>\u7576\u7269\u4ef6\u64c1\u6709\u8a72\u5c6c\u6027\u6642\uff0c\u51fd\u5f0f\u6703\u56de\u50b3 `True`\uff1b\u5426\u5247\uff0c\u5247\u6703\u56de\u50b3 `False`\u3002<\/p>\n<p>### \u7bc4\u4f8b\uff1a\u6aa2\u67e5\u7269\u4ef6\u7684\u5c6c\u6027<\/p>\n<p>\u5047\u8a2d\u6211\u5011\u6709\u4e00\u500b\u7269\u4ef6 `obj`\uff0c\u5b83\u5177\u6709\u4e00\u500b\u5c6c\u6027 `name`\u3002\u6211\u5011\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u7a0b\u5f0f\u78bc\u4f86\u78ba\u8a8d\u8a72\u7269\u4ef6\u662f\u5426\u6709\u6b64\u5c6c\u6027\uff1a<\/p>\n<p>&#8220;`python<br \/>\nclass Person:<br \/>\n    def __init__(self, name):<br \/>\n        self.name = name<\/p>\n<p>p = Person(&#8216;John&#8217;)<\/p>\n<p>if hasattr(p, &#8216;name&#8217;):<br \/>\n    print(&#8216;Person has a name.&#8217;)<br \/>\nelse:<br \/>\n    print(&#8216;Person has no name.&#8217;)<br \/>\n&#8220;`<\/p>\n<p>\u57f7\u884c\u4e0a\u9762\u7684\u7a0b\u5f0f\u78bc\uff0c\u6703\u5f97\u5230\u4ee5\u4e0b\u8f38\u51fa\uff1a<\/p>\n<p>&#8220;`<br \/>\nPerson has a name.<br \/>\n&#8220;`<\/p>\n<p>\u9019\u500b\u7c21\u55ae\u7684\u7bc4\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528 `hasattr()` \u51fd\u5f0f\u4f86\u6aa2\u67e5\u7269\u4ef6\u7684\u5c6c\u6027\u3002<\/p>\n<p>### \u932f\u8aa4\u6392\u9664<\/p>\n<p>\u7576\u4f7f\u7528 `hasattr()` \u6642\uff0c\u53ef\u80fd\u6703\u9047\u5230\u4ee5\u4e0b\u5e38\u898b\u932f\u8aa4\uff1a<\/p>\n<p>1. **\u5c6c\u6027\u540d\u7a31\u932f\u8aa4**\uff1a\u78ba\u4fdd\u4f60\u50b3\u905e\u7684\u5c6c\u6027\u540d\u7a31\u662f\u6b63\u78ba\u7684\u5b57\u4e32\u683c\u5f0f\uff0c\u5426\u5247 `hasattr()` \u6703\u56de\u50b3 `False`\u3002<\/p>\n<p>2. **\u7269\u4ef6\u672a\u5b9a\u7fa9**\uff1a\u5728\u6aa2\u67e5\u5c6c\u6027\u4e4b\u524d\uff0c\u8acb\u78ba\u4fdd\u7269\u4ef6\u5df2\u7d93\u6b63\u78ba\u521d\u59cb\u5316\uff0c\u5426\u5247\u6703\u5f15\u767c `NameError`\u3002<\/p>\n<p>### \u5ef6\u4f38\u61c9\u7528<\/p>\n<p>`hasattr()` \u51fd\u5f0f\u7684\u61c9\u7528\u4e0d\u50c5\u9650\u65bc\u6aa2\u67e5\u5c6c\u6027\uff0c\u9084\u53ef\u4ee5\u7528\u65bc\u52d5\u614b\u5c6c\u6027\u8655\u7406\uff0c\u4f8b\u5982\u5728\u7269\u4ef6\u5c0e\u5411\u8a2d\u8a08\u4e2d\uff0c\u6839\u64da\u7269\u4ef6\u7684\u5c6c\u6027\u9032\u884c\u4e0d\u540c\u7684\u64cd\u4f5c\u3002<\/p>\n<p>### \u7e3d\u7d50<\/p>\n<p>`hasattr()` \u51fd\u5f0f\u662f Python \u4e2d\u4e00\u500b\u5be6\u7528\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u5e6b\u52a9\u958b\u767c\u8005\u6aa2\u67e5\u7269\u4ef6\u7684\u5c6c\u6027\uff0c\u5f9e\u800c\u63d0\u9ad8\u7a0b\u5f0f\u7684\u7a69\u5b9a\u6027\u8207\u53ef\u8b80\u6027\u3002\u8a18\u5f97\u5728\u4f7f\u7528\u6642\u6ce8\u610f\u5c6c\u6027\u540d\u7a31\u7684\u6b63\u78ba\u6027\uff0c\u4ee5\u907f\u514d\u51fa\u73fe\u4e0d\u5fc5\u8981\u7684\u932f\u8aa4\u3002<\/p>\n<p>\u6b64\u5916\uff0c\u82e5\u60f3\u9032\u4e00\u6b65\u4e86\u89e3 Python \u7684\u5176\u4ed6\u529f\u80fd\uff0c\u53ef\u4ee5\u53c3\u8003\u9019\u7bc7\u6587\u7ae0 [Python \u57fa\u790e\u6559\u5b78](https:\/\/vocus.cc\/article\/123456) \u4f86\u64f4\u5c55\u4f60\u7684\u77e5\u8b58\u3002<\/p>\n<p>### Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/p>\n<p>**Q1: `hasattr()` \u51fd\u5f0f\u53ef\u4ee5\u6aa2\u67e5\u54ea\u4e9b\u985e\u578b\u7684\u5c6c\u6027\uff1f**<br \/>\nA1: `hasattr()` \u53ef\u4ee5\u6aa2\u67e5\u4efb\u4f55\u7269\u4ef6\u7684\u5c6c\u6027\uff0c\u5305\u62ec\u57fa\u672c\u6578\u64da\u985e\u578b\u7684\u5c6c\u6027\u3001\u65b9\u6cd5\u5c6c\u6027\u7b49\u3002<\/p>\n<p>**Q2: \u5982\u679c\u7269\u4ef6\u662f `None`\uff0c\u4f7f\u7528 `hasattr()` \u6703\u767c\u751f\u4ec0\u9ebc\uff1f**<br \/>\nA2: \u4f7f\u7528 `hasattr()` \u6aa2\u67e5 `None` \u7269\u4ef6\u7684\u5c6c\u6027\u6703\u5f15\u767c `AttributeError`\uff0c\u56e0\u70ba `None` \u985e\u578b\u6c92\u6709\u5c6c\u6027\u3002<\/p>\n<p>**Q3: `hasattr()` \u662f\u5426\u80fd\u6aa2\u67e5\u79c1\u6709\u5c6c\u6027\uff1f**<br \/>\nA3: \u662f\u7684\uff0c`hasattr()` \u53ef\u4ee5\u6aa2\u67e5\u79c1\u6709\u5c6c\u6027\uff0c\u4f46\u9700\u8981\u4f7f\u7528\u6b63\u78ba\u7684\u547d\u540d\u7d04\u5b9a\uff08\u4ee5\u96d9\u4e0b\u5283\u7dda\u958b\u982d\u7684\u5c6c\u6027\u540d\u7a31\uff09\u4f86\u9032\u884c\u6aa2\u67e5\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>hasattr()\u51fd\u6578\u662fPython\u4e2d\u7684\u4e00\u500b\u5167\u7f6e\u51fd\u6578\uff0c\u7528\u65bc\u6aa2\u67e5\u5c0d\u8c61\u662f\u5426\u5177\u6709\u7d66\u5b9a\u7684\u5c6c\u6027\u3002\u672c\u6587\u5c07\u4ecb\u7d39hasattr()\u51fd\u6578\u7684\u4f7f\u7528\u65b9\u6cd5\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u5b83\u4f86\u6aa2\u67e5\u5c0d\u8c61\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-3006","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-Mu","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3006","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=3006"}],"version-history":[{"count":3,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3006\/revisions"}],"predecessor-version":[{"id":12967,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3006\/revisions\/12967"}],"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=3006"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3006"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3006"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}