Python内置函数(16)——dir
英文文档:
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.
说明:
1. 当不传参数时,返回当前作用域内的变量、方法和定义的类型列表。
>>> dir()
['__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__']
>>> a = 10 #定义变量a
>>> dir() #多了一个a
['__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'a']
2. 当参数对象是模块时,返回模块的属性、方法列表。
>>> import math
>>> math
<module 'math' (built-in)>
>>> dir(math)
['__doc__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']
3. 当参数对象是类时,返回类及其子类的属性、方法列表。
>>> class A:
name = 'class' >>> a = A()
>>> dir(a) #name是类A的属性,其他则是默认继承的object的属性、方法
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'name']
4. 当对象定义了__dir__方法,则返回__dir__方法的结果
>>> class B:
def __dir__(self):
return ['name','age'] >>> b = B()
>>> dir(b) #调用 __dir__方法
['age', 'name']
Python内置函数(16)——dir的更多相关文章
- Python内置函数(40)——dir
英文文档: dir([object]) Without arguments, return the list of names in the current local scope. With an ...
- Python内置函数(16)——ord
英文文档: ord(c) Given a string representing one Unicode character, return an integer representing the U ...
- 16.python内置函数
Python 内置函数:https://www.runoob.com/python/python-built-in-functions.html 原文:https://www.cnblogs.com/ ...
- 【Python】Python内置函数dir详解
1.命令介绍 最近学习并使用了一个python的内置函数dir,首先help一下: 复制代码代码如下: >>> help(dir)Help on built-in function ...
- python内置函数
python内置函数 官方文档:点击 在这里我只列举一些常见的内置函数用法 1.abs()[求数字的绝对值] >>> abs(-13) 13 2.all() 判断所有集合元素都为真的 ...
- python 内置函数和函数装饰器
python内置函数 1.数学相关 abs(x) 取x绝对值 divmode(x,y) 取x除以y的商和余数,常用做分页,返回商和余数组成一个元组 pow(x,y[,z]) 取x的y次方 ,等同于x ...
- 【转】python 内置函数总结(大部分)
[转]python 内置函数总结(大部分) python 内置函数大讲堂 python全栈开发,内置函数 1. 内置函数 python的内置函数截止到python版本3.6.2,现在python一共为 ...
- python内置函数,匿名函数
一.匿名函数 匿名函数:为了解决那些功能很简单的需求而设计的一句话函数 def calc(n): return n**n print(calc(10)) #换成匿名函数 calc = lambda n ...
- python 内置函数总结(大部分)
python 内置函数大讲堂 python全栈开发,内置函数 1. 内置函数 python的内置函数截止到python版本3.6.2,现在python一共为我们提供了68个内置函数.它们就是pytho ...
随机推荐
- systemd-unit
参考: systemd unit 中文手册
- 咸鱼入门到放弃11--Servlet+JSP+JavaBean开发模式
本篇搬运了大佬blog:https://www.cnblogs.com/xdp-gacl/p/3902537.html 一.Servlet+JSP+JavaBean开发模式(MVC)介绍 Servle ...
- Invitation Cards spfa
题意 n个点m条单向边 计算 从1到每一个点的权值(除了1) 和每个点回到1 的权值之和 正向跑一次 反向跑一次即可 #include<bits/stdc++.h> using na ...
- Win10 教育版
Windows 10 版本 1607 引入了专为 K-12 机构的特有需求而设计的两个版本:Windows 10 专业教育版和 Windows 10 教育版. 这些版本为不断发展的 K-12 教育 I ...
- django自制后台左侧导航代码
自定义一个sort.py页面: 写入一下代码: class lanmusort(object): def __init__(self): self.arr=[] def lanmuget(self,o ...
- 针对 jQuery Gridly 控件显示多少列的问题。
针对 jQuery Gridly 控件显示多少列的问题,完全根据 columns 的值来显示. 但是显示columns,并不是给多少值显示几列.到目前还是很模糊的.官方文档没有给出具体的一个解释. $ ...
- POJ3436 Command Network [最小树形图]
POJ3436 Command Network 最小树形图裸题 傻逼poj回我青春 wa wa wa 的原因竟然是需要%.2f而不是.2lf 我还有英语作业音乐作业写不完了啊啊啊啊啊啊啊啊啊 #inc ...
- Android全平台书籍
<Android Database Programming>:全书研究Android平台下的数据库技术. <Android Application Programming with ...
- MyBatis(10)使用association进行分步查询
(1)接口中编写方法 public Emp getEmpByStep(Integer id); public Dept getDeptById(Integer id); (2)Mapper文件 < ...
- 微信小程序之canvas绘制海报分享到朋友圈
绘制canvas内容 首先,需要写一个canvas标签,给canvas-id命名为shareBox <canvas canvas-id="shareBox"></ ...