python中dir()函数怎么用?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
今天我们来看一个非常重要的函数:dir ()
中文说明:不带参数时,返回当前范围内的变量,方法和定义的类型列表;带参数时,返回参数的属性,方法列表。如果参数包含方法__dir__(),该方法将被调用。如果参数不包含__dir__(),该方法将最大限度地收集参数信息。
参数对象:对象,变量,类型。
版本:该函数在python各个版本中都有,但是每个版本中显示的属性细节有所不同。使用时注意区别。
英文说明:
dir((对象)) 时间参数,Without return 从而list of names 拷贝,current local 范围只With an 参数,,attempt 用return a list ,valid attributes  for that 对象。 If 从而,object has a  method named __dir__(),,却;能够method will be nbsp;以及must return 从而list of 属性只 ,allows objects  that implement  a custom __getattr__(),或是__getattribute__ (), function 用customize 从而way dir () reports 才能;their 属性。 If 从而,object does not  provide __dir__(),从而function tries its best 用gather 方式;得到,对象的 s __dict__ 属性,,if 定义,以及得到its type 对象只,resulting list is not necessarily 完成,以及may ,inaccurate when 从而,object has a custom __getattr__ ()。 从而default dir (), mechanism behaves differently with different types of 对象,,as it attempts 用produce 从而most 相关的,,rather than 完成,,信息: If 从而,object is a  module 对象,,,list contains 从而names of 从而模块's 属性。 If 从而,object is a  type 或是class 对象,,,list contains 从而names of its 属性,以及recursively of 从而 attributes of  its 基地。 否则,,,list contains 从而对象's 属性”,名称,从而names of its 类's 属性,以及递归 从而,of  attributes of  its 类's base 类。
例如
在祝辞祝辞import 结构 在祝辞祝辞dir (),, #, show 从而names 拷贝,module 名称空间 [& # 39;__builtins__& # 39; & # 39; __doc__& # 39;, & # 39; __name__ # 39;, & # 39;结构# 39;】 在祝辞祝辞dir(结构),,#,show 从而names 拷贝,struct 模块 [& # 39;结构# 39;& # 39;__builtins__& # 39;, & # 39; __doc__& # 39;, & # 39; __file__ # 39;, & # 39; __name__ # 39;, ,& # 39;__package__& # 39; & # 39; _clearcache& # 39; & # 39; calcsize # 39;, & # 39;错误# 39;& # 39;包# 39;,& # 39;pack_into& # 39; ,& # 39;打开# 39;,& # 39;。# 39; 在祝辞祝辞class 形状(对象): ,,,,,,,def __dir__(自我): ,,,,,,,,,,,return [& # 39;区域# 39;& # 39;周边# 39;,& # 39;位置# 39;】 在祝辞祝辞,s=,形状() 祝辞祝辞祝辞dir (s)
[& # 39;区域# 39;,,& # 39;周边# 39;,,& # 39;位置# 39;】 Note Because  dir (), is supplied primarily as a convenience for use at an interactive 提示,,it tries 用supply an interesting set  of names more  than it tries 用supply a rigorously 或是consistently defined set of 名称,以及its detailed behavior  may change  across 版本只For 例如,,metaclass attributes 断开连接;not 拷贝,result list when 从而 argument is  a 类。
代码实例
在祝辞祝辞dir () [& # 39;__builtins__& # 39; & # 39; __doc__& # 39;, & # 39; __name__ # 39;, & # 39; __package__& # 39;】 在祝辞祝辞import 结构 在祝辞祝辞dir () [& # 39;__builtins__& # 39; & # 39; __doc__& # 39;, & # 39; __name__ # 39;, & # 39; __package__& # 39;, & # 39;结构# 39;】 在祝辞祝辞dir(结构) [& # 39;结构# 39;& # 39;__builtins__& # 39;, & # 39; __doc__& # 39;, & # 39; __file__ # 39;, & # 39; __name__ # 39;, & # 39; __package__& # 39;, & # 39; _clearcache& # 39;, & # 39; calcsize # 39;, & # 39;错误# 39;,& # 39;包# 39;, & # 39;pack_into& # 39; & # 39;打开# 39;,& # 39;。# 39; 在祝辞祝辞class 人(对象): ,,,,,def __dir__(自我): null null null null null null null null nullpython中dir()函数怎么用