Python dir()函数

版本

E:\Projects\testTool>python --version
Python 3.6.2

先看看官网上是怎么描述dir()函数的:
Without arguments, return the list of names in the current local scope. With an argument, attempt to return a list of valid attributes for that object.
翻译过来就是:
如果不向dir()提供参数,返回当前范围的名称列表,带参数时,返回参数的名称列表。

示例

  1. 直接调用dir()
>>> dir()
['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__']
  1. 在当前模块引入一个包
>>> dir()
['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'sys']

执行后的结果包含了引入的包的名字
3.获得sys模块的属性

>>> dir(sys)
['__displayhook__', '__doc__', '__excepthook__', '__interactivehook__', '__loader__', '__name__', '__package__', '__spec__', '__stderr__', '__stdin__', '__stdout__', '_clear_type_cache', '_current_frames', '_debugmallocstats', '_enablelegacywindowsfsencoding', '_getframe', '_git', '_home', '_xoptions', 'api_version', 'argv', 'base_exec_prefix', 'base_prefix', 'builtin_module_names', 'byteorder', 'call_tracing', 'callstats', 'copyright', 'displayhook', 'dllhandle', 'dont_write_bytecode', 'exc_info', 'excepthook', 'exec_prefix', 'executable', 'exit', 'flags', 'float_info', 'float_repr_style', 'get_asyncgen_hooks', 'get_coroutine_wrapper', 'getallocatedblocks', 'getcheckinterval', 'getdefaultencoding', 'getfilesystemencodeerrors', 'getfilesystemencoding', 'getprofile', 'getrecursionlimit', 'getrefcount', 'getsizeof', 'getswitchinterval', 'gettrace', 'getwindowsversion', 'hash_info', 'hexversion', 'implementation', 'int_info', 'intern', 'is_finalizing', 'maxsize', 'maxunicode', 'meta_path', 'modules', 'path', 'path_hooks', 'path_importer_cache', 'platform', 'prefix', 'ps1', 'ps2', 'set_asyncgen_hooks', 'set_coroutine_wrapper', 'setcheckinterval', 'setprofile', 'setrecursionlimit', 'setswitchinterval', 'settrace', 'stderr', 'stdin', 'stdout', 'thread_info', 'version', 'version_info', 'warnoptions', 'winver']

4.在当前模块添加一个变量

>>> a = 5 #创建了一个新变量
>>> dir()
['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'a', 'sys']

5.在当前模块添加一个函数

>>> def function():pass #添加一个函数
>>> dir()
['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'a', 'function', 'sys']

6.在当前模块添加一个类

>>> class Testcls(): #新建一个类
...     def __init__(self):
...         self.a = 1
...         self.b = 2
...     def add_func(self):
...         return self.a + self.b
>>> dir()
['Testcls', '__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'a', 'function', 'sys']

查看类Testcls的属性:

>>> dir(Testcls)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'add_func']

总结

通过以上的示例,python提供的dir()函数,其实就是查看当前域的变量、方法和类,在我们不知道一个模块或库或类有哪些属性时,使用dir()很方便。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,422评论 19 139
  • 要点: 函数式编程:注意不是“函数编程”,多了一个“式” 模块:如何使用模块 面向对象编程:面向对象的概念、属性、...
    victorsungo阅读 5,507评论 0 6
  • 我想你了 可是不能对你说 就像开满桃花的树上 永远结不出青杏 我想你了 可是不知道你在哪 就像夏天的雨 永远看不到...
    宁静致远_6736阅读 1,592评论 0 0
  • 第一天 在医院待了几天,爸爸生病住院了,医生诊断冠心病,住院治疗。 住进去的第一天,病房里已经住了俩个病人了,都是...
    月儿的2016阅读 5,234评论 5 4
  • 从双十一到双旦节,我是月月剁手,月月吃土。最近整理了一下购买记录,发现好多小东西,用起来满满的都是幸福感。下面为你...
    汪炜炜阅读 10,613评论 2 32