最近确实是有些忙,刚过了年,积攒了很多事情需要处理,所以每日一函数只能是每两天更新一篇,在这里和大家致歉。

今天我们来看一个非常重要的函数:dir()

中文说明:不带参数时,返回当前范围内的变量、方法和定义的类型列表;带参数时,返回参数的属性、方法列表。如果参数包含方法__dir__(),该方法将被调用。如果参数不包含__dir__(),该方法将最大限度地收集参数信息。

参数object: 对象、变量、类型。

版本:该函数在python各个版本中都有,但是每个版本中显示的属性细节有所不同。使用时注意区别。

英文说明

dir([object])

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.

If the object has a method named __dir__(), this method will be called and must return the list of attributes. This allows objects that implement a custom __getattr__() or __getattribute__() function to customize the way dir() reports their attributes.

If the object does not provide __dir__(), the function tries its best to gather information from the object’s __dict__ attribute, if defined, and from its type object. The resulting list is not necessarily complete, and may be inaccurate when the object has a custom __getattr__().

The default dir() mechanism behaves differently with different types of objects, as it attempts to produce the most relevant, rather than complete, information:

If the object is a module object, the list contains the names of the module’s attributes.

If the object is a type or class object, the list contains the names of its attributes, and recursively of the attributes of its bases.

Otherwise, the list contains the object’s attributes’ names, the names of its class’s attributes, and recursively of the attributes of its class’s base classes.

The resulting list is sorted alphabetically. For example:

1
2
3
4
5
6
7
8
9
10
11
12
>>> import struct
>>> dir()   # show the names in the module namespace
['__builtins__''__doc__''__name__''struct']
>>> dir(struct)   # show the names in the struct module
['Struct''__builtins__''__doc__''__file__''__name__',
 '__package__''_clearcache''calcsize''error''pack''pack_into',
 'unpack''unpack_from']
>>> class Shape(object):
        def __dir__(self):
            return ['area''perimeter''location']
>>> s = Shape()
>>> dir(s)

['area', 'perimeter', 'location']

Note Because dir() is supplied primarily as a convenience for use at an interactive prompt, it tries to supply an interesting set of names more than it tries to supply a rigorously or consistently defined set of names, and its detailed behavior may change across releases. For example, metaclass attributes are not in the result list when the argument is a class.

特别说明:改系列文章所有代码实例没有特殊说明则都是基于python2.7

代码实例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
>>> dir()
['__builtins__''__doc__''__name__''__package__']
>>> import struct
>>> dir()
['__builtins__''__doc__''__name__''__package__''struct']
>>> dir(struct)
['Struct''__builtins__''__doc__''__file__''__name__''__package__''_clearcache','calcsize''error''pack''pack_into''unpack''unpack_from']
>>> class Person(object):
...     def __dir__(self):
...             return ["name""age""country"]
...
>>> dir(Person)
['__class__''__delattr__''__dict__''__dir__''__doc__''__format__','__getattribute__''__hash__''__init__''__module__''__new__','__reduce__','__reduce_ex__''__repr__''__setattr__''__sizeof__''__str__','__subclasshook__''__weakref__']
>>> tom = Person()
>>> dir(tom)
['age''country''name']

最新文章

  1. Nginx 配置从零开始
  2. ListView的使用-模拟微博随便看看栏目【执行与优化】
  3. nginx反向代理后getRequestURL会出现问题
  4. 使用VB6制作RTD函数
  5. WPF文本框密码框添加水印效果
  6. PHP获取每月第一天与最后一天
  7. hadoop2.2.0 MapReduce分区
  8. EAX、ECX、EDX、EBX寄存器的作用(转)
  9. 阅读<反欺骗的艺术>思考
  10. 时间序列 预测分析 R语言
  11. 全景智慧掌上城,飞入寻常百姓家——VR全景智慧城市
  12. 安装GPU版本的tensorflow填过的那些坑!---CUDA说再见!
  13. Java数据持久层框架 MyBatis之API学习六(Mapper XML 文件详解)
  14. git submoudle提交
  15. Golang 介绍与安装
  16. 转:Flutter动画一
  17. LeetCode算法题-Intersection of Two Arrays II(Java实现)
  18. Python-多表关联 外键 级联
  19. SQL Server 性能优化实战系列(二)
  20. [ Laravel 5.5 文档 ] 官方扩展包 —— 全文搜索解决方案:Laravel Scout

热门文章

  1. SpringBoot非官方教程 | 第二十三篇: 异步方法
  2. [Oracle]Oracle表权限小结
  3. C++最接近整数的浮点运算
  4. boost::asio::ip::tcp中几个重要类型
  5. 洛谷P1709 [USACO5.5]隐藏口令Hidden Password(最小表示法)
  6. jquery和vue分别对input输入框手机号码格式化(344)
  7. PyCharm+QT Designer整合
  8. IDEA项目启动报Unable to open debugger port (127.0.0.1:51554): java.net.SocketException "socket closed"
  9. Leecode刷题之旅-C语言/python-26.移除元素
  10. python2.7入门---函数