Python dir()示例| dir()函数在Python教程中
Python dir()示例| dir()函数在Python中是今天的主题。 Python dir()是Python3中强大的内置函数,它返回任何对象的属性和方法列表。 dir方法尝试返回对象的有效属性列表。
内容概述
- 1 Python目录()
- 2 #dir()函数与Python列表
- 3 #dir()函数与Python元组和字典
- 4#用户定义对象上的dir()
- 5#导入其他库
- 6#使用dir()函数
Python目录()
dir()函数返回指定对象的所有属性和方法,而不返回值。
Python dir()方法将返回所有属性和方法,甚至是所有对象的默认内置属性。
dir()函数的语法如下。
dir(object)
object参数是您要查看其有效属性的对象。
请参阅以下代码示例。
# app.py class Child: name = "El" age = 11 country = "USA" print(dir(Child))
请参阅以下输出。
➜ pyt python3 app.py ('__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__', 'age', 'country', 'name') ➜ pyt
dir()尝试返回对象的有效属性列表。
- 如果对象具有__dir __()方法,则将调用该方法并且必须返回属性列表。
- 如果对象没有__dir __()方法,则此方法尝试从__dict__属性(如果已定义)和类型对象中查找信息。在这种情况下,从dir()返回的列表可能不完整。
如果对象未传递给dir()方法,则返回当前本地范围中的名称列表。
请参阅以下代码。
# app.py print(dir())
请参阅以下输出。
➜ pyt python3 app.py ('__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__') ➜ pyt
#dir()函数与Python列表
让我们定义Python List并使用dir()函数检查以获取列表的所有函数和属性。我们将列出一些数字。
# app.py number = (11, 21, 19) print(dir(number))
请参阅以下输出。
➜ pyt python3 app.py ('__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') ➜ pyt
#dir()函数与Python元组和字典
让我们使用带有Python元组和字典的dir()函数。
# app.py print('See the dictionary') dictA = {'name': 'Eleven', 'show': 'Stranger Things'} print(dir(dictA)) print('See the tuple') tupleA = ('Eleven', 'Alexie', 'Hopper') print(dir(tupleA))
请参阅以下输出。
➜ pyt python3 app.py See the dictionary ('__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values') See the tuple ('__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') ➜ pyt
#The用户定义对象上的dir()
好的,现在让我们创建一个用户定义的对象,然后使用dir()函数。
# app.py class Eleven: def __dir__(self): return ('age', 'name', 'power') supergirl = Eleven() print(dir(supergirl))
请参阅以下输出。
➜ pyt python3 app.py ('age', 'name', 'power') ➜ pyt
#Import其他库
好的,让我们导入数学库并查看数学库的内容。请参阅以下代码。
# app.py import math print(dir(math))
见下面的输出。
➜ pyt python3 app.py ('__doc__', '__file__', '__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', 'tau', 'trunc') ➜ pyt
#dir()函数的使用
- Python dir()有自己的一组用法。它通常用于简单的日常程序中的调试目的,甚至用于开发人员团队占用的大型项目。 dir()函数的功能是列出所传递的参数的所有属性,在分别处理大量类和函数时非常有用。
- dir()函数还可以列出模块/列表/字典的所有可用属性。因此,它还为我们提供了有关我们可以使用列表或模块执行的操作的信息,这对于在该模块上几乎没有信息时非常有用。它还有助于更快地了解新模块。
最后,Python dir()示例| dir()函数在Python教程中结束了。