1.命令介绍

最近学习并使用了一个python的内置函数dir,首先help一下:

>>> help(dir)

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.实例下面是一个简单的测试:

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))

测试结果:

dir without arguments: ['A', 'A1', '__builtins__', '__doc__', '__file__', '__name__', '__package__']

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,再建一个测试程序,内容如下:

import A

if __name__ == '__main__':

print("dir module A:", dir(A))

结果如下:

dir module A: ['A', 'A1', '__builtins__', '__doc__', '__file__', '__name__', '__package__']

可以看出class A和A1都能够找到。

4.如何找到当前模块下的类

这是一个烦恼较长时间的一个问题,也没有搜到详细的解决方法,下面是我的集中实现方法。

4.1.方法一:在module下面直接调用

比如在上面的A.py最下面添加一行,即可在后续的代码中可以使用selfDir来查找当前的module下的类,修改后的代码如下:

class A:

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一样引用,代码如下:

# A.py

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。代码如下:

import sys

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中dir用法_Python内置函数dir详解相关推荐

  1. [转载] python支持complex吗_Python 内置函数complex详解

    参考链接: Python complex() 英文文档: class complex([real[, imag]]) Return a complex number with the value re ...

  2. python中complex函数的用法_Python 内置函数complex详解

    英文文档: class complex([real[, imag]]) Return a complex number with the value real + imag*1j or convert ...

  3. python中的complex是什么意思_Python 内置函数complex详解,pythoncomplex

    Python 内置函数complex详解,pythoncomplex 英文文档: class complex([real[, imag]]) Return a complex number with ...

  4. python int函数详解_Python内置函数OCT详解

    英文文档:oct ( x ) Convert an integer number to an octal string. The result is a valid Python expression ...

  5. python中isinstance用法_Python内置isinstance函数详细介绍

    英文文档: isinstance(object, classinfo) Return true if the object argument is an instance of the classin ...

  6. python中的作用域以及内置函数globals()-全局变量、locals()-局部变量

    在python中,函数会创建一个自己的作用域,也称为为命名空间.这意味着在函数内部访问某个变量时,函数会优先在自己的命名空间中寻找. 通过内置函数globals()返回的是python解释器能知道的变 ...

  7. python学习高级篇(part6)--内置函数dir

    学习笔记,仅供参考,有错必纠 内置函数dir 对于类对象或实例对象,可以调用内置函数dir()获得其所有可以访问的属性和方法(包括从父类中继承的属性和方法)的列表. 类对象与实例对象的结果是有区别的, ...

  8. Python中这两个内置函数locals 和globals,你了解吗?

    这两个函数主要提供,基于字典的访问局部和全局变量的方式. 在理解这两个函数时,首先来理解一下python中的名字空间概念.Python使用叫做名字空间的 东西来记录变量的轨迹.名字空间只是一个字典,它 ...

  9. Python内置函数 max 详解

    python文档中定义了很多内置函数,今天有个同学问到max函数到底在什么情况下可以使用,模模糊糊的记得在序列中都可以使用,但是并不是准确的回答.以下是更详细的内容 一.参数 首先在文档中查看max函 ...

最新文章

  1. How to get pure json data by ajax request
  2. Android实例-MotionSensor加速度(XE8+小米2)
  3. ORACLE使用GV_$TEMP_SPACE_HEADER统计临时表空使用情况不准确的问题
  4. 《系统集成项目管理工程师》必背100个知识点-58沟通方式
  5. python正则表达式实例教程_Python正则表达式经典入门教程
  6. python面向过程是基于面向对象的_Python5.1-面向对象与面向过程
  7. JAVA_java.util.Date与java.sql.Date相互转换
  8. 201712-2-游戏
  9. typedef struct引起的结构体问题
  10. 强烈推荐深入浅出jBPM
  11. 计算机专业新手小白学编程如何选择笔记本电脑
  12. 公司内部分享【富有成效的每日站会】总结
  13. 一小时建立数据分析平台
  14. 只有加法也能做深度学习,北大、华为等提出AdderNet,性能不输传统CNN
  15. html5 canvas 在线图片转换器
  16. 十进制转换为32进制,并反转
  17. 理论力学类毕业论文文献包含哪些?
  18. 《你好,放大器》----学习记录(二)
  19. 清迈府Chiang Mai
  20. NVR新版界面看回放时音频功能如何开启

热门文章

  1. 混沌模型时间序列预测
  2. 区块链BaaS云服务(35)亦笔科技ODRChain
  3. Linux GCC用法
  4. 密码篇——对称加密—DES
  5. Sophos将AI技术用于预防恶意IP的安全解决方案中
  6. 程序员面试拼多多,来看看这些面试题你掌握的有多少呢?
  7. 9、MySQL定义条件和处理程序
  8. 2021算法竞赛入门班第一节课【枚举、贪心】习题
  9. 1071 Speech Patterns (25 分)【难度: 简单 / 知识点: 哈希表 字符串】
  10. Codeforces Round #744 (Div. 3)【A-E1】