{"id":3426,"date":"2025-05-30T14:55:37","date_gmt":"2025-06-04T07:48:36","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3426"},"modified":"2025-06-04T14:55:37","modified_gmt":"2025-06-04T07:48:36","slug":"%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84dir%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%84dir%e5%87%bd%e6%95%b8\/","title":{"rendered":"\u6df1\u5165\u4e86\u89e3 Python \u7684 `dir()` \u51fd\u6578\uff1a2025 \u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, dir()\"><\/p>\n<h1>\u6df1\u5165\u4e86\u89e3 Python \u7684 `dir()` \u51fd\u6578<\/h1>\n<p>Python \u7684 `dir()` \u51fd\u6578\u662f\u4e00\u500b\u975e\u5e38\u6709\u7528\u7684\u5167\u5efa\u51fd\u6578\uff0c\u5b83\u53ef\u4ee5\u7528\u4f86\u67e5\u770b\u7269\u4ef6\u7684\u5c6c\u6027\u548c\u65b9\u6cd5\u3002\u7121\u8ad6\u662f\u6a21\u7d44\u3001\u985e\u5225\u3001\u51fd\u5f0f\u9084\u662f\u7269\u4ef6\u672c\u8eab\uff0c`dir()` \u51fd\u6578\u90fd\u80fd\u5e6b\u52a9\u958b\u767c\u8005\u5feb\u901f\u4e86\u89e3\u5b83\u5011\u7684\u529f\u80fd\u3002\u672c\u6587\u5c07\u4ecb\u7d39 `dir()` \u51fd\u6578\u7684\u4f7f\u7528\u65b9\u6cd5\u3001\u512a\u9ede\u53ca\u5176\u5728\u5be6\u969b\u958b\u767c\u4e2d\u7684\u61c9\u7528\u3002<\/p>\n<h2>2025 \u6700\u65b0\u8a9e\u6cd5\uff1a`dir()` \u51fd\u6578\u7684\u4f7f\u7528\u65b9\u6cd5<\/h2>\n<p>`dir()` \u51fd\u6578\u7684\u4f7f\u7528\u65b9\u6cd5\u975e\u5e38\u7c21\u55ae\u3002\u53ea\u9700\u8981\u5728\u7a0b\u5f0f\u78bc\u4e2d\u8abf\u7528 `dir()` \u51fd\u6578\uff0c\u4e26\u5c07\u8981\u67e5\u770b\u7684\u7269\u4ef6\u4f5c\u70ba\u53c3\u6578\u50b3\u5165\uff0c\u5373\u53ef\u5f97\u5230\u8a72\u7269\u4ef6\u7684\u5c6c\u6027\u548c\u65b9\u6cd5\u5217\u8868\u3002\u4e0b\u9762\u662f\u4e00\u4e9b\u5be6\u4f5c\u7bc4\u4f8b\uff1a<\/p>\n<p>&#8220;`python<br \/>\n# \u67e5\u770b list \u7269\u4ef6\u7684\u5c6c\u6027\u548c\u65b9\u6cd5<br \/>\nlist_example = [1, 2, 3]\nprint(dir(list_example))<\/p>\n<p># \u8f38\u51fa\u7d50\u679c\u793a\u4f8b<br \/>\n# [&#8216;__add__&#8217;, &#8216;__class__&#8217;, &#8216;__contains__&#8217;, &#8216;__delattr__&#8217;, &#8216;__delitem__&#8217;, &#8216;__dir__&#8217;,<br \/>\n#  &#8216;__doc__&#8217;, &#8216;__eq__&#8217;, &#8216;__format__&#8217;, &#8216;__ge__&#8217;, &#8216;__getattribute__&#8217;, &#8216;__getitem__&#8217;,<br \/>\n#  &#8216;__gt__&#8217;, &#8216;__hash__&#8217;, &#8216;__iadd__&#8217;, &#8216;__imul__&#8217;, &#8216;__init__&#8217;, &#8216;__init_subclass__&#8217;,<br \/>\n#  &#8216;__iter__&#8217;, &#8216;__le__&#8217;, &#8216;__len__&#8217;, &#8216;__lt__&#8217;, &#8216;__mul__&#8217;, &#8216;__ne__&#8217;, &#8216;__new__&#8217;,<br \/>\n#  &#8216;__reduce__&#8217;, &#8216;__reduce_ex__&#8217;, &#8216;__repr__&#8217;, &#8216;__reversed__&#8217;, &#8216;__rmul__&#8217;,<br \/>\n#  &#8216;__setattr__&#8217;, &#8216;__setitem__&#8217;, &#8216;__sizeof__&#8217;, &#8216;__str__&#8217;, &#8216;__subclasshook__&#8217;,<br \/>\n#  &#8216;append&#8217;, &#8216;clear&#8217;, &#8216;copy&#8217;, &#8216;count&#8217;, &#8216;extend&#8217;, &#8216;index&#8217;, &#8216;insert&#8217;, &#8216;pop&#8217;,<br \/>\n#  &#8216;remove&#8217;, &#8216;reverse&#8217;, &#8216;sort&#8217;]\n&#8220;`<\/p>\n<p>\u5728\u9019\u6bb5\u7a0b\u5f0f\u78bc\u4e2d\uff0c\u6211\u5011\u4f7f\u7528 `dir()` \u51fd\u6578\u67e5\u770b\u4e86 `list` \u7269\u4ef6\u7684\u6240\u6709\u5c6c\u6027\u548c\u65b9\u6cd5\uff0c\u9019\u5c0d\u65bc\u4e86\u89e3 Python \u4e2d\u7684\u8cc7\u6599\u7d50\u69cb\u975e\u5e38\u6709\u5e6b\u52a9\u3002<\/p>\n<h2>`dir()` \u51fd\u6578\u7684\u512a\u9ede<\/h2>\n<p>`dir()` \u51fd\u6578\u7684\u4e3b\u8981\u512a\u9ede\u5728\u65bc\u5b83\u80fd\u8b93\u958b\u767c\u8005\u8fc5\u901f\u4e86\u89e3\u7269\u4ef6\u7684\u529f\u80fd\uff0c\u800c\u4e0d\u9700\u8981\u67e5\u95b1\u5b98\u65b9\u6587\u4ef6\u6216\u9032\u884c\u7db2\u8def\u641c\u5c0b\u3002\u9019\u80fd\u5927\u5927\u63d0\u5347\u958b\u767c\u6548\u7387\uff0c\u7279\u5225\u662f\u5728\u8655\u7406\u5927\u578b\u5c08\u6848\u6216\u4e0d\u719f\u6089\u7684\u5eab\u6642\u3002<\/p>\n<p>\u6b64\u5916\uff0c`dir()` \u51fd\u6578\u4e0d\u50c5\u9650\u65bc\u5167\u5efa\u7269\u4ef6\uff0c\u9084\u53ef\u4ee5\u7528\u65bc\u81ea\u5b9a\u7fa9\u985e\u5225\u3002\u4f8b\u5982\uff1a<\/p>\n<p>&#8220;`python<br \/>\nclass MyClass:<br \/>\n    def __init__(self):<br \/>\n        self.attribute = &#8220;Hello&#8221;<\/p>\n<p>    def method(self):<br \/>\n        return &#8220;World&#8221;<\/p>\n<p># \u67e5\u770b MyClass \u7684\u5c6c\u6027\u548c\u65b9\u6cd5<br \/>\nobj = MyClass()<br \/>\nprint(dir(obj))<br \/>\n&#8220;`<\/p>\n<p>\u9019\u5c07\u5217\u51fa `MyClass` \u7684\u6240\u6709\u5c6c\u6027\u548c\u65b9\u6cd5\uff0c\u5305\u62ec `attribute` \u548c `method`\u3002<\/p>\n<h2>\u932f\u8aa4\u6392\u9664\u8207\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u5728\u4f7f\u7528 `dir()` \u51fd\u6578\u6642\uff0c\u6709\u6642\u53ef\u80fd\u6703\u9047\u5230\u4e00\u4e9b\u56f0\u96e3\uff0c\u4f8b\u5982\uff1a<\/p>\n<p>&#8211; **\u7269\u4ef6\u4e0d\u5b58\u5728**\uff1a\u78ba\u4fdd\u50b3\u5165 `dir()` \u7684\u7269\u4ef6\u5df2\u88ab\u6b63\u78ba\u521d\u59cb\u5316\u3002<br \/>\n&#8211; **\u7121\u6cd5\u67e5\u770b\u5c6c\u6027**\uff1a\u67d0\u4e9b\u5c6c\u6027\u53ef\u80fd\u662f\u79c1\u6709\u7684\uff0c\u7121\u6cd5\u901a\u904e `dir()` \u67e5\u770b\u3002<\/p>\n<p>\u91dd\u5c0d\u9019\u4e9b\u554f\u984c\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u8003\u616e\u4f7f\u7528 `getattr()` \u51fd\u6578\u4f86\u7372\u53d6\u5c6c\u6027\u503c\uff0c\u6216\u4f7f\u7528 `help()` \u51fd\u6578\u7372\u53d6\u66f4\u591a\u8cc7\u8a0a\u3002<\/p>\n<p>\u5982\u679c\u4f60\u60f3\u9032\u4e00\u6b65\u4e86\u89e3 Python \u7684\u5167\u5efa\u51fd\u6578\uff0c\u5efa\u8b70\u53c3\u8003 [\u9019\u7bc7\u6559\u5b78\u6587\u7ae0](https:\/\/vocus.cc\/article\/6410e0f5fd89780001d2e442)\uff0c\u5b83\u63d0\u4f9b\u4e86\u66f4\u591a\u5be6\u7528\u7684\u793a\u4f8b\u548c\u8a73\u7d30\u89e3\u91cb\u3002<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>Python \u7684 `dir()` \u51fd\u6578\u662f\u4e00\u500b\u975e\u5e38\u5f37\u5927\u7684\u5de5\u5177\uff0c\u80fd\u5920\u5e6b\u52a9\u958b\u767c\u8005\u5feb\u901f\u4e86\u89e3\u7269\u4ef6\u7684\u5c6c\u6027\u548c\u65b9\u6cd5\uff0c\u9032\u800c\u63d0\u5347\u958b\u767c\u6548\u7387\u3002\u7121\u8ad6\u662f\u5728\u5b78\u7fd2\u65b0\u6a21\u7d44\u9084\u662f\u958b\u767c\u5927\u578b\u5c08\u6848\u6642\uff0c`dir()` \u51fd\u6578\u90fd\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u5e6b\u624b\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<p>**Q1: `dir()` \u51fd\u6578\u53ef\u4ee5\u7528\u4f86\u67e5\u770b\u54ea\u4e9b\u7269\u4ef6\u7684\u5c6c\u6027\u548c\u65b9\u6cd5\uff1f**<br \/>\nA1: `dir()` \u51fd\u6578\u53ef\u4ee5\u7528\u4f86\u67e5\u770b\u5404\u7a2e\u7269\u4ef6\u7684\u5c6c\u6027\u548c\u65b9\u6cd5\uff0c\u5305\u62ec\u5167\u5efa\u985e\u5225\u3001\u6a21\u7d44\u3001\u81ea\u5b9a\u7fa9\u985e\u5225\u7b49\u3002<\/p>\n<p>**Q2: \u5982\u4f55\u67e5\u770b\u4e00\u500b\u6a21\u7d44\u7684\u6240\u6709\u5c6c\u6027\u548c\u65b9\u6cd5\uff1f**<br \/>\nA2: \u4f7f\u7528 `import` \u5c0e\u5165\u6a21\u7d44\u5f8c\uff0c\u8abf\u7528 `dir(\u6a21\u7d44\u540d\u7a31)` \u5c31\u80fd\u5217\u51fa\u8a72\u6a21\u7d44\u7684\u6240\u6709\u5c6c\u6027\u548c\u65b9\u6cd5\u3002<\/p>\n<p>**Q3: `dir()` \u51fd\u6578\u7684\u8f38\u51fa\u7d50\u679c\u53ef\u4ee5\u505a\u4ec0\u9ebc\uff1f**<br \/>\nA3: \u8f38\u51fa\u7d50\u679c\u53ef\u4ee5\u5e6b\u52a9\u958b\u767c\u8005\u4e86\u89e3\u7269\u4ef6\u7684\u53ef\u7528\u65b9\u6cd5\uff0c\u9032\u884c\u6709\u6548\u7684\u7a0b\u5f0f\u78bc\u958b\u767c\u8207\u8abf\u8a66\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>dir()\u51fd\u6578\u662fPython\u4e2d\u7684\u4e00\u500b\u5167\u5efa\u51fd\u6578\uff0c\u53ef\u4ee5\u7528\u4f86\u67e5\u770b\u7269\u4ef6\u3001\u5217\u8868\u3001\u5b57\u5178\u7b49\u7684\u5c6c\u6027\u548c\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u5feb\u901f\u5730\u4e86\u89e3\u7269\u4ef6\u7684\u529f\u80fd\uff0c\u4e26\u66f4\u6709\u6548\u5730\u4f7f\u7528Python\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-3426","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-Tg","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3426","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=3426"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3426\/revisions"}],"predecessor-version":[{"id":3427,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3426\/revisions\/3427"}],"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=3426"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3426"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3426"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}