{"id":3314,"date":"2025-05-29T15:39:18","date_gmt":"2025-06-04T08:15:18","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3314"},"modified":"2025-06-04T15:39:18","modified_gmt":"2025-06-04T08:15:18","slug":"%e4%ba%86%e8%a7%a3python%e7%9a%84dir%e5%87%bd%e6%95%b8-4","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e4%ba%86%e8%a7%a3python%e7%9a%84dir%e5%87%bd%e6%95%b8-4\/","title":{"rendered":"\u6df1\u5165\u63a2\u7d22 Python \u7684 dir() \u51fd\u6578\uff1a\u7528\u6cd5\u8207\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, dir(), Python \u6559\u5b78, Python \u51fd\u6578\"><\/p>\n<h1>\u6df1\u5165\u63a2\u7d22 Python \u7684 dir() \u51fd\u6578\uff1a\u7528\u6cd5\u8207\u6700\u4f73\u5be6\u8e10<\/h1>\n<p>Python \u7684 `dir()` \u51fd\u6578\u662f\u4e00\u500b\u5f37\u5927\u7684\u5de5\u5177\uff0c\u80fd\u5920\u5e6b\u52a9\u958b\u767c\u8005\u5feb\u901f\u67e5\u770b\u7269\u4ef6\u7684\u5c6c\u6027\u548c\u65b9\u6cd5\uff0c\u4ee5\u53ca\u6a21\u7d44\u4e2d\u5b9a\u7fa9\u7684\u540d\u7a31\u3002\u672c\u6587\u5c07\u4ecb\u7d39 `dir()` \u51fd\u6578\u7684\u57fa\u672c\u7528\u6cd5\u8207\u9ad8\u7d1a\u6280\u5de7\uff0c\u4e26\u63d0\u4f9b\u5be6\u4f5c\u7bc4\u4f8b\uff0c\u5e6b\u52a9\u60a8\u638c\u63e1\u9019\u4e00\u91cd\u8981\u51fd\u6578\u3002<\/p>\n<h2>\u4ec0\u9ebc\u662f dir() \u51fd\u6578\uff1f<\/h2>\n<p>`dir()` \u51fd\u6578\u662f Python \u7684\u5167\u5efa\u51fd\u6578\u4e4b\u4e00\uff0c\u4e3b\u8981\u7528\u65bc\u52d5\u614b\u67e5\u770b\u7269\u4ef6\u7684\u6240\u6709\u5c6c\u6027\u548c\u65b9\u6cd5\u3002\u9019\u5c0d\u65bc\u8abf\u8a66\u548c\u4e86\u89e3\u7b2c\u4e09\u65b9\u6a21\u7d44\u6216\u81ea\u5b9a\u7fa9\u7269\u4ef6\u7279\u5225\u6709\u5e6b\u52a9\u3002<\/p>\n<h2>\u5982\u4f55\u4f7f\u7528 dir() \u51fd\u6578\uff1f<\/h2>\n<p>\u4f7f\u7528 `dir()` \u51fd\u6578\u5341\u5206\u7c21\u55ae\u3002\u60a8\u53ea\u9700\u5728 Python \u4ea4\u4e92\u5f0f\u547d\u4ee4\u884c\u6216\u8173\u672c\u4e2d\u50b3\u5165\u60f3\u8981\u6aa2\u67e5\u7684\u7269\u4ef6\u6216\u6a21\u7d44\uff0c\u5373\u53ef\u7372\u5f97\u5176\u6240\u6709\u5c6c\u6027\u548c\u65b9\u6cd5\u7684\u5217\u8868\u3002<\/p>\n<pre class=\"brush: python\">\n# \u67e5\u770b\u5b57\u4e32\u7269\u4ef6\u7684\u5c6c\u6027\u548c\u65b9\u6cd5\nprint(dir(str))\n\n# \u67e5\u770b math \u6a21\u7d44\u7684\u5c6c\u6027\u548c\u65b9\u6cd5\nimport math\nprint(dir(math))\n<\/pre>\n<h2>dir() \u51fd\u6578\u7684\u57fa\u672c\u7528\u6cd5<\/h2>\n<p>`dir()` \u51fd\u6578\u7684\u57fa\u672c\u7528\u6cd5\u662f\u63a5\u6536\u4e00\u500b\u7269\u4ef6\u6216\u6a21\u7d44\u4f5c\u70ba\u53c3\u6578\uff0c\u7136\u5f8c\u8fd4\u56de\u8a72\u7269\u4ef6\u6216\u6a21\u7d44\u7684\u5c6c\u6027\u548c\u65b9\u6cd5\u5217\u8868\u3002<\/p>\n<pre class=\"brush: python\">\n# \u50b3\u5165\u4e00\u500b\u7269\u4ef6\nobject_attributes = dir(object)\n\n# \u50b3\u5165\u4e00\u500b\u6a21\u7d44\nmodule_attributes = dir(module)\n<\/pre>\n<h2>dir() \u51fd\u6578\u7684\u9ad8\u7d1a\u7528\u6cd5<\/h2>\n<p>\u9664\u4e86\u57fa\u672c\u7528\u6cd5\uff0c`dir()` \u51fd\u6578\u9084\u652f\u63f4\u9ad8\u7d1a\u529f\u80fd\uff0c\u8b93\u60a8\u53ef\u4ee5\u67e5\u8a62\u7279\u5b9a\u7684\u5c6c\u6027\u548c\u65b9\u6cd5\u3002<\/p>\n<pre class=\"brush: python\">\n# \u67e5\u770b\u7279\u5b9a\u7684\u5c6c\u6027\u548c\u65b9\u6cd5\nspecific_attributes = dir(object, 'attribute_name')\n\n# \u67e5\u770b\u6a21\u7d44\u4e2d\u5b9a\u7fa9\u7684\u540d\u7a31\nmodule_names = dir(module, 'name')\n<\/pre>\n<h2>\u5be6\u4f5c\u7bc4\u4f8b<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u5be6\u4f5c\u7bc4\u4f8b\uff0c\u5e6b\u52a9\u60a8\u66f4\u597d\u5730\u7406\u89e3 `dir()` \u51fd\u6578\u7684\u4f7f\u7528\u3002\u5047\u8a2d\u6211\u5011\u60f3\u8981\u67e5\u770b\u81ea\u5b9a\u7fa9\u985e\u5225\u7684\u5c6c\u6027\u548c\u65b9\u6cd5\uff1a<\/p>\n<pre class=\"brush: python\">\nclass MyClass:\n    def __init__(self):\n        self.attribute1 = 'value1'\n    \n    def method1(self):\n        return 'method1 called'\n\n# \u5275\u5efa MyClass \u7684\u5be6\u4f8b\nmy_object = MyClass()\n\n# \u4f7f\u7528 dir() \u67e5\u770b\u5c6c\u6027\u548c\u65b9\u6cd5\nprint(dir(my_object))\n<\/pre>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5982\u679c\u5728\u4f7f\u7528 `dir()` \u51fd\u6578\u6642\u9047\u5230\u932f\u8aa4\uff0c\u8acb\u6aa2\u67e5\u4ee5\u4e0b\u5e7e\u9ede\uff1a<\/p>\n<p>1. \u78ba\u4fdd\u50b3\u5165\u7684\u53c3\u6578\u662f\u6709\u6548\u7684\u7269\u4ef6\u6216\u6a21\u7d44\u3002<br \/>\n2. \u78ba\u8a8d\u6a21\u7d44\u662f\u5426\u5df2\u7d93\u6b63\u78ba\u5c0e\u5165\u3002<br \/>\n3. \u6aa2\u67e5 Python \u74b0\u5883\u662f\u5426\u6b63\u78ba\u914d\u7f6e\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>`dir()` \u51fd\u6578\u4e0d\u50c5\u80fd\u67e5\u770b\u7269\u4ef6\u7684\u5c6c\u6027\uff0c\u9084\u53ef\u4ee5\u5e6b\u52a9\u60a8\u5feb\u901f\u4e86\u89e3\u81ea\u5b9a\u7fa9\u985e\u5225\u548c\u7b2c\u4e09\u65b9\u5eab\u7684\u7d50\u69cb\u3002\u9019\u5c0d\u65bc\u7de8\u5beb\u6e2c\u8a66\u548c\u6587\u6a94\u975e\u5e38\u6709\u5e6b\u52a9\u3002\u70ba\u4e86\u6df1\u5165\u4e86\u89e3 Python \u7684\u5176\u4ed6\u5167\u5efa\u51fd\u6578\uff0c\u60a8\u53ef\u4ee5\u53c3\u8003\u9019\u7bc7[Python \u51fd\u6578\u5927\u5168](https:\/\/vocus.cc\/article\/642e7f8cfd89780001d9a4f0)\u3002<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>Python \u7684 `dir()` \u51fd\u6578\u662f\u958b\u767c\u8005\u5728\u9032\u884c\u8abf\u8a66\u548c\u5b78\u7fd2\u65b0\u6a21\u7d44\u6642\u4e0d\u53ef\u6216\u7f3a\u7684\u5de5\u5177\u3002\u900f\u904e\u5b83\uff0c\u60a8\u53ef\u4ee5\u5feb\u901f\u4e86\u89e3\u4efb\u4f55\u7269\u4ef6\u6216\u6a21\u7d44\u7684\u7d50\u69cb\uff0c\u4e26\u6709\u6548\u63d0\u5347\u60a8\u7684\u958b\u767c\u6548\u7387\u3002\u5e0c\u671b\u672c\u6587\u80fd\u5e6b\u52a9\u60a8\u66f4\u597d\u5730\u638c\u63e1 `dir()` \u51fd\u6578\u7684\u7528\u6cd5\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<p>**Q1: dir() \u51fd\u6578\u662f\u5426\u80fd\u67e5\u770b\u6240\u6709\u7269\u4ef6\u7684\u5c6c\u6027\uff1f**<br \/>\nA1: \u662f\u7684\uff0c`dir()` \u51fd\u6578\u53ef\u4ee5\u67e5\u770b\u50b3\u5165\u7269\u4ef6\u7684\u6240\u6709\u5c6c\u6027\u548c\u65b9\u6cd5\uff0c\u5305\u62ec\u81ea\u5b9a\u7fa9\u985e\u5225\u7684\u5c6c\u6027\u548c\u7b2c\u4e09\u65b9\u6a21\u7d44\u7684\u5c6c\u6027\u3002<\/p>\n<p>**Q2: \u4f7f\u7528 dir() \u51fd\u6578\u6642\uff0c\u70ba\u4ec0\u9ebc\u6211\u770b\u4e0d\u5230\u67d0\u4e9b\u5c6c\u6027\uff1f**<br \/>\nA2: \u6709\u4e9b\u5c6c\u6027\u53ef\u80fd\u662f\u79c1\u6709\u7684\uff0c\u6216\u662f\u672a\u5728\u7269\u4ef6\u7684\u547d\u540d\u7a7a\u9593\u4e2d\u5b9a\u7fa9\uff0c\u60a8\u53ef\u4ee5\u6aa2\u67e5\u5c0d\u61c9\u7684\u6587\u6a94\u6216\u4f7f\u7528 `help()` \u51fd\u6578\u4f86\u7372\u53d6\u66f4\u591a\u4fe1\u606f\u3002<\/p>\n<p>**Q3: dir() \u51fd\u6578\u7684\u8fd4\u56de\u7d50\u679c\u53ef\u4ee5\u9032\u884c\u7be9\u9078\u55ce\uff1f**<br \/>\nA3: `dir()` \u51fd\u6578\u672c\u8eab\u4e0d\u652f\u63f4\u7be9\u9078\uff0c\u4f46\u60a8\u53ef\u4ee5\u5c07\u8fd4\u56de\u7684\u5217\u8868\u8207\u5176\u4ed6 Python \u51fd\u6578\u7d50\u5408\u4f7f\u7528\uff0c\u5982\u5217\u8868\u63a8\u5c0e\u5f0f\uff0c\u4f86\u9032\u884c\u9032\u4e00\u6b65\u7684\u7be9\u9078\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>dir()\u51fd\u6578\u662fPython\u4e2d\u7684\u4e00\u500b\u91cd\u8981\u51fd\u6578\uff0c\u5b83\u53ef\u4ee5\u7528\u4f86\u67e5\u770b\u7269\u4ef6\u7684\u5c6c\u6027\u548c\u65b9\u6cd5\uff0c\u672c\u6587\u5c07\u4ecb\u7d39dir()\u51fd\u6578\u7684\u4f7f\u7528\u65b9\u6cd5\uff0c\u4ee5\u53ca\u5982\u4f55\u5229\u7528\u5b83\u4f86\u66f4\u597d\u5730\u7ba1\u7406Python\u7a0b\u5f0f\u78bc\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-3314","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-Rs","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3314","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=3314"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3314\/revisions"}],"predecessor-version":[{"id":3315,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3314\/revisions\/3315"}],"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=3314"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3314"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3314"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}