dir()函数以及Python中的示例

Python dir() 功能

dir()函数是一个库函数,用于获取对象的所有属性和方法的列表,它接受一个对象并返回所有内置的,用户定义的属性,不带值的方法的列表。

语法:

    dir(object)

Parameter(s): object-一个对象,它需要我们的属性和方法。

返回值: list –返回所有属性和方法的列表。

示例

    Input:
    class std_info:
      name = "Amit shukla"
      age = 21
      course = "B.Tech (CS)"

    # printing details using dir() function
    print(dir(std_info))
    
    Output:
    ['__class__', '__delattr__', '__dict__'...
    ...'age', 'course', 'name']

Python代码获取对象的属性,方法列表

# python代码演示示例 
# dir() number

class std_info:
  name = "Amit shukla"
  age = 21
  course = "B.Tech (CS)"

# printing return type of dir() function
print("return type of dir(): ", type(dir(std_info)))

# printing details using dir() function
print(dir(std_info))

输出结果

return type of dir():  <class 'list'>
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', 
'__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', 
'__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', 
'__new__', '__reduce__', '__reduce_ex__', '__repr__', '__se
tattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 
'age', 'course', 'name']

Python代码获取属性列表,整数方法和float对象

# python代码演示示例 
# dir() number

a = 10
b = 10.23

print("a has...")
print(dir(a))

print("b has...")
print(dir(a))

输出结果

a has...
['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delattr__', 
'__dir__', '__divmod__', '__doc__', '__eq  __', '__float__', '__floor__', '__floordiv__', 
'__format__', '__ge__', '__getattribute__', '__getnewargs__', '__gt__', '__hash_  
_', '__index__', '__init__', '__int__', '__invert__', '__le__', '__lshift__', '__lt__', 
'__mod__', '__mul__', '__ne__', '__neg_  _', '__new__', '__or__', '__pos__', '__pow__', 
'__radd__', '__rand__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__'  , 
'__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__round__', '__rpow__', 
'__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', 
'__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', 
'__xor__', 'bit_length', 'conjugate', 'denominator', 'from_bytes', 
'imag', 'numerator', 'real', 'to_bytes']              

b has...
['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delattr__', 
'__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floor__', '__floordiv__', 
'__format__', '__ge__', '__getattribute__', '__getnewargs__', '__gt__', '__hash__', 
'__index__', '__init__', '__int__', '__invert__', '__le__', '__lshift__', '__lt__', 
'__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__or__', '__pos__', '__pow__', 
'__radd__', '__rand__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__' , 
'__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__round__', 
'__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', 
'__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', 
'__trunc__', '__xor__', 'bit_length', 'conjugate', 'denominator', 
'from_bytes', 'imag', 'numerator', 'real', 'to_bytes']