python中dir函数
Python dir()函数 (Python dir() function)
dir() function is a library function, it is used to get the list of all properties and methods of an object, it accepts an object and returns a list of all inbuilt, user-defined properties, methods without the values.
dir()函数是一个库函数,用于获取对象的所有属性和方法的列表,它接受一个对象并返回所有内置的,用户定义的属性,没有值的方法的列表。
Syntax:
句法:
dir(object)
Parameter(s): object – an object whose properties and methods we want.
参数: object –一个需要其属性和方法的对象。
Return value: list – it returns a list of all properties and methods.
返回值: list –返回所有属性和方法的列表。
Example:
例:
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 code to get the list of properties, methods of an object
Python代码获取对象的属性,方法列表
# python code to demonstrate example of
# 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))
Output
输出量
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 code to get list of properties, method of an integer and a float object
使用Python代码获取属性列表,整数方法和float对象
# python code to demonstrate example of
# dir() number
a = 10
b = 10.23
print("a has...")
print(dir(a))
print("b has...")
print(dir(a))
Output
输出量
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']
翻译自: https://www.includehelp.com/python/dir-function-with-example.aspx
python中dir函数