{"id":179226,"date":"2023-07-29T10:57:09","date_gmt":"2023-07-29T02:57:09","guid":{"rendered":"http:\/\/www.idc.net\/help\/179226\/"},"modified":"2023-07-29T10:57:09","modified_gmt":"2023-07-29T02:57:09","slug":"%e5%90%8e%e6%b5%aa%e4%ba%91python%e6%95%99%e7%a8%8b%ef%bc%9apython%e6%80%8e%e4%b9%88%e6%9f%a5%e7%9c%8b%e5%b8%ae%e5%8a%a9%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/idc.net\/help\/179226\/","title":{"rendered":"\u540e\u6d6a\u4e91Python\u6559\u7a0b\uff1apython\u600e\u4e48\u67e5\u770b\u5e2e\u52a9\u51fd\u6570"},"content":{"rendered":"<p style=\"text-align: center\"><img decoding=\"async\" src=\"https:\/\/oss.py.cn\/pycn\/upload\/article\/000\/000\/004\/5da53ccee52e9449.jpg\" class=\"aligncenter\"><\/p>\n<p>python\u4e2d\u7684dir()\u51fd\u6570\uff0c\u53ef\u4ee5\u5728\u6211\u4eec\u4e0d\u4e86\u89e3\u67d0\u4e00\u51fd\u6570\u6216\u65b9\u6cd5\u65f6\uff0c\u5e2e\u6211\u4eec\u67e5\u770b\u8fd9\u4e9b\u65b9\u6cd5\u6216\u51fd\u6570\u7684\u7528\u6cd5\u53ca\u5c5e\u6027\u3002<\/p>\n<p>dir()\u51fd\u6570\u8bf4\u660e<\/p>\n<pre>\u4e0d\u5e26\u53c2\u6570\u65f6\uff0c\u8fd4\u56de\u5f53\u524d\u8303\u56f4\u5185\u7684\u53d8\u91cf\u3001\u65b9\u6cd5\u548c\u5b9a\u4e49\u7684\u7c7b\u578b\u5217\u8868\uff1b\u5e26\u53c2\u6570\u65f6\uff0c\u8fd4\u56de\u53c2\u6570\u7684\u5c5e\u6027\u3001\u65b9\u6cd5\u5217\u8868\u3002\u5982\u679c\u53c2\u6570\u5305\u542b\u65b9\u6cd5__dir__()\uff0c\n\u8be5\u65b9\u6cd5\u5c06\u88ab\u8c03\u7528\u3002\u5982\u679c\u53c2\u6570\u4e0d\u5305\u542b__dir__()\uff0c\u8be5\u65b9\u6cd5\u5c06\u9650\u5ea6\u5730\u6536\u96c6\u53c2\u6570\u4fe1\u606f\u3002<\/pre>\n<p>\u53c2\u6570object\uff1a \u5bf9\u8c61\u3001\u53d8\u91cf\u3001\u7c7b\u578b\u3002<\/p>\n<p>\u7248\u672c\uff1a\u8be5\u51fd\u6570\u5728python\u5404\u4e2a\u7248\u672c\u4e2d\u90fd\u6709\uff0c\u4f46\u662f\u6bcf\u4e2a\u7248\u672c\u4e2d\u663e\u793a\u7684\u5c5e\u6027\u7ec6\u8282\u6709\u6240\u4e0d\u540c\u3002\u4f7f\u7528\u65f6\u6ce8\u610f\u533a\u522b\u3002<\/p>\n<p>\u4f8b\u5982<\/p>\n<pre>&gt;&gt;&gt;import&nbsp;struct\n&gt;&gt;&gt;dir()&nbsp;&nbsp;#&nbsp;show&nbsp;the&nbsp;names&nbsp;in&nbsp;the&nbsp;module&nbsp;namespace\n['__builtins__','__doc__','__name__','struct']\n&gt;&gt;&gt;dir(struct)&nbsp;&nbsp;#&nbsp;show&nbsp;the&nbsp;names&nbsp;in&nbsp;the&nbsp;struct&nbsp;module\n['Struct','__builtins__','__doc__','__file__','__name__',\n&nbsp;'__package__','_clearcache','calcsize','error','pack','pack_into',\n&nbsp;'unpack','unpack_from']\n&gt;&gt;&gt;class&nbsp;Shape(object):\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def&nbsp;__dir__(self):\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;['area','perimeter','location']\n&gt;&gt;&gt;&nbsp;s=&nbsp;Shape()\n&gt;&gt;&gt;dir(s)<\/pre>\n<p>\u4ee3\u7801\u5b9e\u4f8b<\/p>\n<pre>&gt;&gt;&gt;dir()\n['__builtins__','__doc__','__name__','__package__']\n&gt;&gt;&gt;import&nbsp;struct\n&gt;&gt;&gt;dir()\n['__builtins__','__doc__','__name__','__package__','struct']\n&gt;&gt;&gt;dir(struct)\n['Struct','__builtins__','__doc__','__file__','__name__','__package__','_clearcache','calcsize','error','pack',\n'pack_into','unpack','unpack_from']\n&gt;&gt;&gt;class&nbsp;Person(object):\n...&nbsp;&nbsp;&nbsp;&nbsp;def&nbsp;__dir__(self):\n...&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;[\"name\",\"age\",\"country\"]\n...\n&gt;&gt;&gt;dir(Person)\n['__class__','__delattr__','__dict__','__dir__','__doc__','__format__','__getattribute__','__hash__','__init__',\n'__module__','__new__','__reduce__','__reduce_ex__','__repr__','__setattr__','__sizeof__','__str__','__subclasshook__',\n'__weakref__']\n&gt;&gt;&gt;&nbsp;tom=&nbsp;Person()\n&gt;&gt;&gt;dir(tom)\n['age','country','name']<\/pre>\n<p>\u66f4\u591a\u5b66\u4e60\u5185\u5bb9\uff0c\u8bf7\u70b9\u51fbPython\u5b66\u4e60\u7f51\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>python\u4e2d\u7684dir()\u51fd\u6570\uff0c\u53ef\u4ee5\u5728\u6211\u4eec\u4e0d\u4e86\u89e3\u67d0\u4e00\u51fd\u6570\u6216\u65b9\u6cd5\u65f6\uff0c\u5e2e\u6211\u4eec\u67e5\u770b\u8fd9\u4e9b\u65b9\u6cd5\u6216\u51fd\u6570\u7684\u7528\u6cd5\u53ca\u5c5e\u6027\u3002 di [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":179227,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[182397],"tags":[],"class_list":["post-179226","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/179226","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/comments?post=179226"}],"version-history":[{"count":0,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/179226\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media\/179227"}],"wp:attachment":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media?parent=179226"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/categories?post=179226"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/tags?post=179226"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}