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

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函数

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值