【Python】python函数每日一讲 - dir()
最近确实是有些忙,刚过了年,积攒了很多事情需要处理,所以每日一函数只能是每两天更新一篇,在这里和大家致歉。
今天我们来看一个非常重要的函数: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'] |
【Python】python函数每日一讲 - dir()的更多相关文章
- Python 全栈开发四 python基础 函数
一.函数的基本语法和特性 函数的定义 函数一词来源于数学,但编程中的「函数」概念,与数学中的函数是有很大不同的.函数是指将一组语句的集合通过一个名字(函数名)封装起来,要想执行这个函数,只需调用其函数 ...
- python的函数
函数一词起源于数学,但是在编程中的函数和数学中的有很大不同.编程中的函数式组织好的,可重复使用的,用于实现单一功能或相关联功能的代码块. 我们在学习过程中已经使用过一些python内建的函数,如pri ...
- Python回调函数用法实例详解
本文实例讲述了Python回调函数用法.分享给大家供大家参考.具体分析如下: 一.百度百科上对回调函数的解释: 回调函数就是一个通过函数指针调用的函数.如果你把函数的指针(地址)作为参数传递给另一个函 ...
- Python之函数与变量
本节内容 函数介绍及其作用 函数的定义与调用 函数的参数说明 全局变量与局部变量 值传递和引用传递 一.函数的介绍及其作用 编程语言中的函数与数学中的函数是有区别的:数学中的函数有参数(输入),就会有 ...
- Python之函数进阶
本节内容 上一篇中介绍了Python中函数的定义.函数的调用.函数的参数以及变量的作用域等内容,现在来说下函数的一些高级特性: 递归函数 嵌套函数与闭包 匿名函数 高阶函数 内置函数 总结 一.递归函 ...
- python 定义函数
在Python中,定义一个函数要使用def语句,依次写出函数名.括号.括号中的参数和冒号:,然后,在缩进块中编写函数体,函数的返回值用return语句返回. 我们以自定义一个求绝对值的my_abs函数 ...
- Python入门-函数的使用到程序的公布安装
Python入门-函数的使用到Python的公布安装 本文主要适合有一定编程经验,至少掌握一门编程语言的人查看. 文中样例大多都是简单到认识英文单词就能看懂的水平,主要讲的是Python的总体使用方法 ...
- python常用函数年初大总结
1.常用内置函数:(不用import就可以直接使用) help(obj) 在线帮助, obj可是任何类型 callable(obj) 查看一个obj是不是可以像函数一样调用 repr(obj) 得到o ...
- python基础—函数装饰器
python基础-函数装饰器 1.什么是装饰器 装饰器本质上是一个python函数,它可以让其他函数在不需要做任何代码变动的前提下增加额外功能. 装饰器的返回值是也是一个函数对象. 装饰器经常用于有切 ...
随机推荐
- 如何配置Java环境变量
百度经验 | 百度知道 | 百度首页 | 登录 | 注册 新闻 网页 贴吧 知道 经验 音乐 图片 视频 地图 百科 文库 帮助 发布经验 首页 分类 任务 回享 商城 特色 知道 百度经验 &g ...
- [Oracle]分区索引
上一节学习了分区表,接着学习分区索引. (一)什么时候对索引进行分区 · 为了避免移动数据时重建整个索引,可对索引分区,在重建索引时,只需重建与数据分区相关的索引: · 在对分区表进行维护时,为了避免 ...
- 伪造Http请求IP地址
注意:伪造Http请求IP地址一般为非推荐使用手段 一般使用:简单投票网站重复投票,黑别人网站 在项目开发中(web项目),我负责的系统(简称PC),需要调其它系统接口,并且该系统需要获取客户端(浏览 ...
- .net 导出Excel插件Npoi的使用
1.NuGet搜索Npoi并安装 2.添加引用将包引用进来 3.Controller里引用 4.使用 public ActionResult ExportExcel() { plist = 数据源 H ...
- Javascript中的this对象
对于this的使用,我们最常遇到的主要有,在全局函数中,在对象方法中,call和apply时,闭包中,箭头函数中以及class中: 我们知道this对象是在运行时基于函数的执行环境绑定的,在调用函数之 ...
- C++创建一个名为Ellipse的椭圆类--练习
题目描述: /*设计名为Ellipse的椭圆类*/ /* 其属性为外接矩形的左上角与右下角两个点的坐标,并能计算出椭圆的面积,并测试该类. */ 代码如下: #include<iostream& ...
- Co. - Microsoft - Windows - Dos命令
DOS命令 cd .. 是进入上一层目录,cd \ 是进入根目录 我们来重申下%~dp0和%cd%的区别, %cd%和%~dp0都能用来表示当前目录,但是他们在不同的使用场景下,功能却不相同: %cd ...
- 分别使用原生js和jQuery添加/删除元素的class属性
一.原生js添加/删除元素的class属性: <!-- span元素原有class = "test" --> <span class="test&quo ...
- 利用html2canvas将当前网页保存为图片.
先分析下这个技术可实现的方式,以及优缺点吧! 前端实现 缺点是:兼容性查,需要高级浏览器支持,因为需要支持 canvas 绘图,还有就是会操作 html5 canvas api.(如果不会使用canv ...
- Linux 控制台
shell shell命令分为两种:分别是内部命令和外部命令. 内部命令:在安装的时候嵌入系统内核. 外部命令:以文件的形式存在. 可以使用type命令查看是内部命令还是外部命令. Linux中,默认 ...