python dir 函数_dir函数python_Python dir()函数

dir函数python

Python dir() function attempts to return a list of valid attributes for the given object. If no argument provided, then it returns the list of names in the current local scope.

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

python dir() (Python dir())

Python dir() function syntax is:

Python dir()函数语法为:

dir([object])

If the object contains __dir__() function, then this function will be called. This function must return the list of attributes.

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

The list of attribute names is sorted in alphabetical order.

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

Let’s look at some examples of dir() function.

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

dir()没有参数 (dir() with no argument)

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

print(dir())

Output:

输出:

dir() with no argument

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

Note that the output depends on the program and python installation, so don’t worry if it’s different in your system.

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

dir()与元组和列表 (dir() with tuple and list)

print('\ndir() with tuple argument\n')

t = (1, 2)

print(dir(t))

print('\ndir() with list argument\n')

l = [1, 2]

print(dir(l))

Output:

输出:

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']

Note that the elements in the list are sorted alphabetically.

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

dir()与自定义对象 (dir() with custom object)

class Data:

id = 0

name = ''

print('\ndir() with custom object argument\n')

d = Data()

print(dir(d))

Output:

输出:

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() with module object)

Let’s see the output of dir() function with a module object, I am using namedtuple from collections module.

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

from collections import namedtuple

n = namedtuple('Vowels', 'a,e,i,o,u')

print('\ndir() with module object argument\n')

print(dir(n))

Output:

输出:

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']

Note that the module’s attributes are also returned by dir() function.

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

带有__dir __()函数的dir() (dir() with __dir__() function)

Let’s define a class with __dir__() function and see if it’s called by dir() function or not.

让我们用__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))

Output:

输出:

dir() with __dir__ method defined in object

__dir__() function called

['Blue', 'Green', 'Red']

摘要 (Summary)

Python dir() is a convinience function to determine the available attributes of an object or the attributes of local scope.

Python dir()是一种便捷函数,用于确定对象的可用属性或局部范围的属性。

GitHub Repository.

GitHub存储库中检出完整的python脚本和更多Python示例。

Reference: Official Documentation

参考: 官方文档

翻译自: https://www.journaldev.com/22810/python-dir-function

dir函数python

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值