【补充】python中的dir函数

dir() 是一个 Python 内置函数,它用于列出对象的所有属性和方法。

当没有参数传递给 dir() 时,它返回当前作用域中所有可用的名称。

以下是 dir() 函数的用法示例:

python 复制代码
# 列出当前作用域中的所有名称
print(dir())  # ['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__']

# 列出指定对象的所有属性和方法
my_list = [1, 2, 3]
print(dir(my_list))

输出示例:

python 复制代码
['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__']


['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', 
'__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']

第一个示例展示了在当前作用域中的名称列表。第二个示例展示了列表对象的所有属性和方法,包括 appendclearcopy 等方法。

相关推荐
Sylvia-girl3 小时前
Java——抽象类
java·开发语言
Yana.nice5 小时前
Bash函数详解
开发语言·chrome·bash
江沉晚呤时6 小时前
在 C# 中调用 Python 脚本:实现跨语言功能集成
python·microsoft·c#·.net·.netcore·.net core
电脑能手7 小时前
如何远程访问在WSL运行的Jupyter Notebook
ide·python·jupyter
tomorrow.hello7 小时前
Java并发测试工具
java·开发语言·测试工具
Edward-tan7 小时前
CCPD 车牌数据集提取标注,并转为标准 YOLO 格式
python
晓13138 小时前
JavaScript加强篇——第四章 日期对象与DOM节点(基础)
开发语言·前端·javascript
老胖闲聊8 小时前
Python I/O 库【输入输出】全面详解
开发语言·python
倔强青铜三8 小时前
苦练Python第18天:Python异常处理锦囊
人工智能·python·面试
倔强青铜三8 小时前
苦练Python第17天:你必须掌握的Python内置函数
人工智能·python·面试