Python dir()

dir 函数将一个对象作为输入,并返回该对象的所有属性。属性以列表的形式返回。

 **dir(object)** # object is any python object 

dir()函数将一个对象作为参数。内置对象和用户定义对象可以作为参数传递。即使没有传递参数,它也不会抛出错误。

参数描述必需/可选
目标要返回其属性的任何 Python 对象可选择的

函数返回传递的对象的所有属性,内置属性也返回。如果没有传递参数,则返回当前本地范围内的名称。

| 投入 | 返回值 |
| 没有人 | 返回当前本地范围内的名称列表。 |
| 目标 | 返回传递的对象的属性 |

 letters = {'a':1, 'b':2, 'c':3}
# Dictionary is a built in object in python 
print(dir(letters)) 

输出:

[' 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'] 
 print(dir()) 

输出:

['    annotations    ', '    builtins    ', '    cached    ', '    doc    ', '    file    ', '    loader    ', '  name ', ' package ', ' spec '] 
 class Student:
name = "Ram" 
age = 22
course = "Bachelors" 
Student1 = Student() 
print(dir(Student1)) 

输出:

['    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', 'course', 'name']
# The built in attributes of class is also returned along with the user defined attributes

原创文章,作者:Y475M,如若转载,请注明出处:https://www.506064.com/n/126711.html

(0)
Y475MY475M
上一篇 2024-10-03
下一篇 2024-10-03

相关推荐

发表回复

登录后才能评论