Python dir() 函数列出了所有属性和方法,包括指定对象的默认属性。在这里,对象可以是任何模块、函数、字符串、列表、字典等。
如果没有参数传递给 dir(),它将返回当前本地作用域中的名称列表。提供参数后,它将返回该对象的有效属性列表,但不包含值。
dir() 函数是内置函数之一,不需要导入任何模块。
语法
以下是 python dir() 函数的语法。
dir(object)
参数
以下是 python dir() 函数的参数 -
- object − 此参数指定要列出其属性的对象。
返回值
python dir() 函数返回指定对象的属性列表。
dir() 函数示例
练习以下示例来理解 Python 中 dir() 函数的用法:
示例:不带任何参数的 dir() 函数
当我们打印 dir() 的值而不传递任何参数时,我们会得到作为标准库的一部分可用的方法和属性的列表。
print("dir method without using any arguments")
print(dir())
在执行上述程序时,将生成以下输出 -
dir method without using any arguments
['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'traceback']
['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'traceback']
示例:具有用户定义类的 dir() 函数
如果我们将用户定义类的对象传递给 dir() 函数,它将显示该对象的属性和方法。它还包括那些内置属性,这些属性是该特定对象的默认属性。
class newClass:
def __init__(self):
self.x = 55
self._y = 44
def newMethod(self):
pass
newObj = newClass()
print("List of methods and properties of given class:")
print(dir(newObj))
以下是执行上述程序得到的输出 -
List of methods and properties of given class:
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_y', 'newMethod', 'x']
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_y', 'newMethod', 'x']
示例:使用 dir() 打印模块的属性和方法
dir() 函数允许我们显示 Python 内置模块的属性和方法,如下面的代码所示。
import math
print("List of methods and properties of MATH module:")
print(dir(math))
通过执行上述程序获得以下输出 -
List of methods and properties of MATH module:
['__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'cbrt', 'ceil', 'comb', 'copysign', 'cos', 'cosh', 'degrees', 'dist', 'e', 'erf', 'erfc', 'exp', 'exp2', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'isqrt', 'lcm', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'nextafter', 'perm', 'pi', 'pow', 'prod', 'radians', 'remainder', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc', 'ulp']
['__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'cbrt', 'ceil', 'comb', 'copysign', 'cos', 'cosh', 'degrees', 'dist', 'e', 'erf', 'erfc', 'exp', 'exp2', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'isqrt', 'lcm', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'nextafter', 'perm', 'pi', 'pow', 'prod', 'radians', 'remainder', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc', 'ulp']
示例:检查父类的继承方法和属性
如果我们想检查父类的继承方法和属性,那么我们可以通过传递子类的 obejct 来使用 dir() 函数。
class Pclass:
def pMethod(self):
pass
class Chclass(Pclass):
def chMethod(self):
pass
chObj = Chclass()
print("List of methods and properties of Parent class:")
print(dir(chObj))
上述程序在执行时显示以下输出 -
List of methods and properties of Parent class:
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'chMethod', 'pMethod']
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'chMethod', 'pMethod']