【补充】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 等方法。

相关推荐
心中有国也有家2 小时前
GE图引擎深度解析——CANN的计算图优化与执行引擎
人工智能·pytorch·python·学习·numpy
卷毛的技术笔记3 小时前
告别硬编码!Spring AI Alibaba 实现 AI Agent 智能工具调用(Tool Calling)
java·人工智能·后端·python·spring·ai编程
编程大师哥3 小时前
匿名函数 lambda + 高阶函数
java·python·算法
isyangli_blog3 小时前
OpenDayLight (Carbon 版本) 启动与组件安装
开发语言·php
vb2008113 小时前
FastAPI APIRouter
开发语言·python
Benszen3 小时前
KVM虚拟化解决方案
开发语言·perl
会编程的土豆3 小时前
Go 语言反射(Reflection)详解
开发语言·后端·golang
東雪木3 小时前
多线程与并发编程 专属复习笔记
java·开发语言·笔记·java面试
adrninistrat0r3 小时前
Java调用链MCP分析工具
java·python·ai编程
杨充4 小时前
1.3 浮点型数据设计灵魂
开发语言·python·算法