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

相关推荐
devilnumber5 小时前
Java 递归算法 详解 + 核心要点 + 实战运用 + 避坑指南
java·开发语言·算法
大貔貅喝啤酒6 小时前
Python Requests库教程
自动化测试·python·requests库
copyer_xyf7 小时前
LangChain 调用 LLM
后端·python·agent
copyer_xyf7 小时前
Prompt 组织管理
后端·python·agent
asdfg12589637 小时前
JavaBean是什么?怎么理解?有什么用途?
java·开发语言
dsyyyyy11017 小时前
JavaScript变量
开发语言·javascript·ecmascript
shimly1234567 小时前
python3 uvicorn 是啥?
python
z落落8 小时前
C#WinForm 窗体切换与窗体传值(登录跳转案例)+WinForm 窗体传值(从上往下传、从下往上传)
开发语言·windows·c#
CTA量化套保8 小时前
期货量化程序 time.sleep 卡死:天勤单线程与 deadline 替代
python·区块链
allway28 小时前
How to Echo Multiline to a File in Bash [3 Methods]
开发语言·chrome·bash