【补充】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']

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

相关推荐
谙弆悕博士1 小时前
多智能体架构详解:市场拍卖式架构(Market-based / 竞价协商)
开发语言·架构·去中心化·多智能体·智能体·拍卖架构
ShineWinsu1 小时前
对于Qt:QMessageBox的解析
开发语言·c++·qt·面试·客户端·qmessagebox·消息对话框
承渊政道2 小时前
【Python编程快速上手—让繁琐工作自动化】(列表:把数据排成队,从下标取值到深拷贝)
开发语言·python·pycharm·自动化·列表
大胖爱学习_MJ2 小时前
Agent 如何安全地拿数据库密码?基于 HashiCorp Vault 的动态凭证下发
经验分享·python
tryCbest2 小时前
Python 爬虫从入门到实战(一):基础篇——从零写出第一个爬虫
开发语言·爬虫·python
无小道2 小时前
Python语句的特色
python·语句
多加点辣也没关系3 小时前
JavaScript|第40章:JavaScript 工程化实践
开发语言·javascript·ecmascript
2601_967980113 小时前
python pytest如何编写测试
python·测试用例·pytest·函数·assert
2601_967980114 小时前
Python中heapq模块的各种用法实例
数据结构·python·算法·应用实例·heapq模块
大胖爱学习_MJ4 小时前
超越向量检索:用 Spring Boot + Neo4j 手搓 GraphRAG 知识图谱记忆
java·后端·python