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

相关推荐
Warson_L17 小时前
Python `Annotated` 与 LangGraph Reducer 学习笔记
python
韩师傅17 小时前
海天线算法的前世今生
python·计算机视觉
韩师傅17 小时前
当你的甲方设备过烂,要如何快速出效果?
python·计算机视觉
Warson_L17 小时前
LangGraph的MessageState and HumanMessage
python
韩师傅17 小时前
当你的甲方吐槽天空不够蓝,你应该如何应对
python·计算机视觉
Warson_L18 小时前
python的类&继承
python
Warson_L18 小时前
类型标注/type annotation
python
ThreeS20 小时前
手搓MiniVLA全实战教程-一步一步用pytorch解释原理与思路
人工智能·python
金銀銅鐵1 天前
[Python] 模 n 乘法的逆元计算器
python·数学·游戏