dir()函数
dir()函数的语法格式:
dir([object])
参数 object是对象、变量、类型。
返回值是模块的属性列表。
如下所示:
- # -*- coding: utf-8 -*-
- """
- @File : func_dir.py
- @Author : 小地瓜重新去华容道工作
- @E-Mail : zoya.zh@qq.com
- @Time : 22/8/28
- """
-
- print(dir()) # 获取当前模块的属性列表
-
- print(dir(list())) # 获取列表的方法
-
- class MyTestDir:
-
-
- def __init__(self):
- self.name="dir"
-
-
- def myPrint(self):
- print("dir() test...")
-
-
- print(dir(MyTestDir())) # 获取类MyTestDir的属性和方法
显示结果:
- ['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__']
- ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
- ['__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__', 'myPrint', 'name']
注意:
__xxx__格式的方法是python提供的内置方法或属性。