【Python】Python内置函数dir详解
1.命令介绍
最近学习并使用了一个python的内置函数dir,首先help一下:
Help on built-in function dir in module __builtin__:
dir()
dir([object]) -> list of strings
Return an alphabetized list of names comprising (some of) the attributes
of the given object, and of attributes reachable from it:
No argument: the names in the current scope.
Module object: the module attributes.
Type or class object: its attributes, and recursively the attributes of
its bases.
Otherwise: its attributes, its class's attributes, and recursively the
attributes of its class's base classes.
通过help,可以简单的认为dir列出指定对象或类的属性。
2.实例
下面是一个简单的测试:
def a(self):
pass
class A1(A):
def a1(self):
pass
if __name__ == '__main__':
print("dir without arguments:", dir())
print("dir class A:", dir(A))
print("dir class A1:", dir(A1))
a = A1()
print("dir object a(A1):", dir(a))
print("dir function a.a:", dir(a.a))
测试结果:
dir class A: ['__class__', '__delattr__', '__dict__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'a']
dir class A1: ['__class__', '__delattr__', '__dict__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'a', 'a1']
dir object a(A1): ['__class__', '__delattr__', '__dict__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'a', 'a1']
dir function a.a: ['__call__', '__class__', '__delattr__', '__doc__', '__eq__', '__format__', '__func__', '__ge__', '__get__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__self__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']
3.使用dir查找module下的所有类
最初使用这个函数的初衷,就是在一个module中查找实现的类名,通过该函数可以很容易的实现。
比如把上面的测试程序保存为A.py,再建一个测试程序,内容如下:
if __name__ == '__main__':
print("dir module A:", dir(A))
结果如下:
可以看出class A和A1都能够找到。
4.如何找到当前模块下的类
这是一个烦恼较长时间的一个问题,也没有搜到详细的解决方法,下面是我的集中实现方法。
4.1.方法一:在module下面直接调用
比如在上面的A.py最下面添加一行,即可在后续的代码中可以使用selfDir来查找当前的module下的类,修改后的代码如下:
def a(self):
pass
class A1(A):
def a1(self):
pass
curModuleDir=dir() # get dir of current file(module)
if __name__ == '__main__':
print("dir without arguments:", dir())
print("dir class A:", dir(A))
print("dir class A1:", dir(A1))
a = A1()
print("dir object a(A1):", dir(a))
print("dir function a.a:", dir(a.a))
print("dir current file:", curModuleDir)
4.2.方法二:import当前module
把当前module和别的import一样引用,代码如下:
import A as this # import current module
class A:
def a(self):
pass
class A1(A):
def a1(self):
pass
if __name__ == '__main__':
print("dir without arguments:", dir())
print("dir class A:", dir(A))
print("dir class A1:", dir(A1))
a = A1()
print("dir object a(A1):", dir(a))
print("dir function a.a:", dir(a.a))
print("dir current file:", dir(this))
4.3.方法三:根据module名称查找module,然后调用dir
我们知道module下面有个属性__name__显示module名称,怎么能够根据module名称来查找module对象呢?可以借助sys.modules。代码如下:
class A:
def a(self):
pass
class A1(A):
def a1(self):
pass
if __name__ == '__main__':
print("dir without arguments:", dir())
print("dir class A:", dir(A))
print("dir class A1:", dir(A1))
a = A1()
print("dir object a(A1):", dir(a))
print("dir function a.a:", dir(a.a))
print("dir current file:", dir(sys.modules[__name__])) # 使用__name__获取当前module对象,然后使用对象获得dir
如对本文有所疑问,请点击进入脚本之家知识社区提问。
您可能感兴趣的文章:
【Python】Python内置函数dir详解的更多相关文章
- day14内置函数作业详解
day14题目 day14作业及默写 1,整理今天所学内容,整理知识点,整理博客. 2,画好流程图. 3,都完成的做一下作业(下面题都是用内置函数或者和匿名函数结合做出): 4,用map来处理字符串列 ...
- python基础-内置函数详解
一.内置函数(python3.x) 内置参数详解官方文档: https://docs.python.org/3/library/functions.html?highlight=built#ascii ...
- python基础——内置函数
python基础--内置函数 一.内置函数(python3.x) 内置参数详解官方文档: https://docs.python.org/3/library/functions.html?highl ...
- 如何查看Python的内置函数
经常调用的时候不知道python当前版本的内置函数是哪些,可以用下面的指令查看: C:\Users\Administrator>python Python 2.7.11 (v2.7.11:6d1 ...
- Python的内置函数
python的内置函数一共有68个,下面将简单介绍各个函数的功能. abs() dict() help() min() setattr() all() dir() hex() next() slice ...
- Python入门-内置函数一
什么是内置函数?就是python给你提供的拿来直接用的函数,比如print,input等等,截止到python版本3.6.2 python一共提供了68个内置函数,他们就是python直接提供给我们的 ...
- python_way,day3 集合、函数、三元运算、lambda、python的内置函数、字符转换、文件处理
python_way,day3 一.集合 二.函数 三.三元运算 四.lambda 五.python的内置函数 六.字符转换 七.文件处理 一.集合: 1.集合的特性: 特性:无序,不重复的序列 如果 ...
- python print()内置函数
啦啦啦啦啦啦,我又来了,学习任何东西都得坚持,我一定的好好加油!!! 今天来说说print()函数,前边我们已经用过好好多次啦,现在来学习哈吧!!! Python的内置函数,print() print ...
- Python 集合内置函数大全(非常全!)
Python集合内置函数操作大全 集合(s).方法名 等价符号 方法说明 s.issubset(t) s <= t 子集测试(允许不严格意义上的子集):s 中所有的元素都是 t 的成员 s ...
随机推荐
- Golang学习笔记(一)
一段基础的go语言代码解析 package main import "fmt" func main(){ fmt.Println("hello golang") ...
- NEC 框架规范 css function
/* function */.f-cb:after,.f-cbli li:after{display:block;clear:both;visibility:hidden;height:0;overf ...
- CentOS 7 以上防火墙简单配置
CentOS 7 版本以上默认的防火墙不是iptables,而是firewalle. 因此CentOS 7 以下的 iptables 的配置不能使用. 查看防火墙状态: systemctl statu ...
- django+xadmin在线教育平台(十六)
7-7 modelform 提交我要学习咨询1 对应表userask form会对字段先做验证,然后保存到数据库中. 可以看到我们的forms和我们的model中有很多内容是一样的.我们如何让代码重复 ...
- 清除input框的缓存
html <div class="container"> <form class="parent" autocomplete="of ...
- js onsubmit和return false的关系
一直以来,我都是以为onsubmit=“return false”就不会进行提交,但经过项目之后才知道return false只是避免了之后的跳转,但onsubmit已经是正在进行了,故onsubmi ...
- JavaSE 第二次学习随笔(三)
* 常见异常 * 数组越界异常 * 空指针异常 * * * 特点: 当程序出现异常的时候, 程序会打印异常信息并中断程序 * 所以当同时出现多个异常的时候只能执行第一个, 后边的用不到 * * 单异常 ...
- Hive优化之谓词下推
Hive优化之谓词下推 解释 Hive谓词下推(Predicate pushdown) 关系型数据库借鉴而来,关系型数据中谓词下推到外部数据库用以减少数据传输 基本思想:尽可能早的处理表达式 属于逻辑 ...
- Logistic Regression学习笔记
1.李航<统计学习方法>: 2.https://blog.csdn.net/laobai1015/article/details/78113214 3.http://www.cnblogs ...
- 什么是mysql数据库安全 简单又通俗的mysql库安全简介
首先我们要了解一下什么是mysql数据库,mysql是目前网站以及APP应用上用的较多的一个开源的关系型数据库系统,可以对数据进行保存,分段化的数据保存,也可以对其数据进行检索,查询等功能的数据库. ...