Python dir()函数

时间:2020-02-23 14:42:39  来源:igfitidea点击:

Python dir()函数尝试返回给定对象的有效属性列表。
如果未提供任何参数,则它将返回当前本地范围内的名称列表。

python dir()

Python dir()函数语法为:

dir([object])

如果对象包含__dir __()函数,则将调用此函数。
此函数必须返回属性列表。

属性名称列表按字母顺序排序。

我们来看一些dir()函数的示例。

dir()没有参数

print('\ndir() with no argument\n')
print(dir())

输出:

dir() with no argument

['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__']

请注意,输出取决于程序和python安装,因此请放心,如果系统中的输出不同。

dir()与元组和列表

print('\ndir() with tuple argument\n')
t = (1, 2)
print(dir(t))

print('\ndir() with list argument\n')
l = [1, 2]
print(dir(l))

输出:

dir() with tuple argument

['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'count', 'index']

dir() with list argument

['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

请注意,列表中的元素按字母顺序排序。

dir()与自定义对象

class Data:
  id = 0
  name = ''

print('\ndir() with custom object argument\n')
d = Data()
print(dir(d))

输出:

dir() with custom object argument

['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'id', 'name']

dir()与模块对象

让我们看一下dir()函数与模块对象的输出,我正在使用collections模块中的namedtuple。

from collections import namedtuple

n = namedtuple('Vowels', 'a,e,i,o,u')
print('\ndir() with module object argument\n')
print(dir(n))

输出:

dir() with module object argument

['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__module__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__sizeof__', '__slots__', '__str__', '__subclasshook__', '_asdict', '_fields', '_fields_defaults', '_make', '_replace', 'a', 'count', 'e', 'i', 'index', 'o', 'u']

请注意,模块的属性也由dir()函数返回。

带有__dir __()函数的dir()

让我们用__dir __()函数定义一个类,看看它是否被dir()函数调用。

class Color:

  def __dir__(self):
      print('__dir__() function called')
      return ['Red', 'Green', 'Blue']

print('\ndir() with __dir__ method defined in object\n')
c = Color()
print(dir(c))

输出:

dir() with __dir__ method defined in object

__dir__() function called
['Blue', 'Green', 'Red']