python入门必会的助手函数:dir()函数

今天我们来看一个非常重要的函数:dir()

中文说明:不带参数时,返回当前范围内的变量、方法和定义的类型列表;带参数时,返回参数的属性、方法列表。如果参数包含方法__dir__(),该方法将被调用。如果参数不包含__dir__(),该方法将限度地收集参数信息。

参数object: 对象、变量、类型。

版本:该函数在python各个版本中都有,但是每个版本中显示的属性细节有所不同。使用时注意区别。

英文说明:

dir([object])Without arguments, return the list of names in the current local scope. With an argument, attempt to return a list ofvalid attributes for that object.If the object has a method named __dir__(), this method will be called and must return the list of attributes. Thisallows objects that implement a custom __getattr__() or __getattribute__() function to customize the way dir()reports their attributes.If the object does not provide __dir__(), the function tries its best to gather information from the object’
s __dict__ attribute, if defined, and from its type object. The resulting list is not necessarily complete, and may beinaccurate when the object has a custom __getattr__().The default dir() mechanism behaves differently with different types of objects, as it attempts to produce the most 
relevant, rather than complete, information:If the object is a module object, the list contains the names of the module’s attributes.If the object is a type or class object, the list contains the names of its attributes, and recursively of the 
attributes of its bases.Otherwise, the list contains the object’s attributes’ names, the names of its class’s attributes, and recursivelyof the attributes of its class’s base classes.

例如

>>>import struct
>>>dir()  # show the names in the module namespace
['__builtins__','__doc__','__name__','struct']
>>>dir(struct)  # show the names in the struct module
['Struct','__builtins__','__doc__','__file__','__name__','__package__','_clearcache','calcsize','error','pack','pack_into','unpack','unpack_from']
>>>class Shape(object):def __dir__(self):return ['area','perimeter','location']
>>> s= Shape()
>>>dir(s)
['area', 'perimeter', 'location']
Note Because dir() is supplied primarily as a convenience for use at an interactive prompt, it tries to supply an 
interesting set of names more than it tries to supply a rigorously or consistently defined set of names, and its 
detailed behavior may change across releases. For example, metaclass attributes are not in the result list when the 
argument is a class.

代码实例

>>>dir()
['__builtins__','__doc__','__name__','__package__']
>>>import struct
>>>dir()
['__builtins__','__doc__','__name__','__package__','struct']
>>>dir(struct)
['Struct','__builtins__','__doc__','__file__','__name__','__package__','_clearcache','calcsize','error','pack',
'pack_into','unpack','unpack_from']
>>>class Person(object):
...    def __dir__(self):
...            return ["name","age","country"]
...
>>>dir(Person)
['__class__','__delattr__','__dict__','__dir__','__doc__','__format__','__getattribute__','__hash__','__init__',
'__module__','__new__','__reduce__','__reduce_ex__','__repr__','__setattr__','__sizeof__','__str__','__subclasshook__',
'__weakref__']
>>> tom= Person()
>>>dir(tom)
['age','country','name']

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/723883.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

IntelliJ IDEA 下载安装及配置使用教程

一、IDEA下载 1、打开游览器输入IntelliJ IDEA – the Leading Java and Kotlin IDE (jetbrains.com) 2、点击Download,进入IDEA下载界面 3、 有两个版本,一个是Ultimate 版本为旗舰版,需要付费,包括完整的功能,下载后…

【JSON2WEB】08 Amis的事件和校验

【JSON2WEB】01 WEB管理信息系统架构设计 【JSON2WEB】02 JSON2WEB初步UI设计 【JSON2WEB】03 go的模板包html/template的使用 【JSON2WEB】04 amis低代码前端框架介绍 【JSON2WEB】05 前端开发三件套 HTML CSS JavaScript 速成 【JSON2WEB】06 JSON2WEB前端框架搭建 【J…

部署DNS解析服务

一、安装软件,关闭防火墙,启动服务 1.yum install -y bind bind-utils bind-chroot 2.systemctl stop firewalld && setenforce 0 3.systemctl start named 二、工作目录 /var/named/chroot/etc #存放主配置文件 /var/named/chroot/var/n…

鸿蒙Harmony应用开发—ArkTS声明式开发(基础手势:LongPressGesture)

用于触发长按手势事件,触发长按手势的最少手指数为1,最短长按时间为500毫秒。 说明: 从API Version 7开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。 接口 LongPressGesture(value?: { fingers?: num…

Vue3:OptionsAPI 与 CompositionAPI的比较

1、Vue2 Vue2的API设计是Options(配置)风格的。 Options API 的弊端 Options类型的 API,数据、方法、计算属性等,是分散在:data、methods、computed中的,若想新增或者修改一个需求,就需要分别…

【Leetcode每日一题】 前缀和 - 和为 K 的子数组(难度⭐)(29)

1. 题目解析 题目链接:560. 和为 K 的子数组 这个问题的理解其实相当简单,只需看一下示例,基本就能明白其含义了。 核心在于计算题目所给数组是否存在连续子数组和为指定值,存在返回连续子数组个数即可,不存在返回0即…

C++入门全集(5):内存管理

前言 一、内存区域划分 二、C的内存管理方式 2.1 对内置类型 2.2 对自定义类型 三、new和delete的底层实现 四、new和delete的原理 五、定位new 六、malloc/free和new/delete 前言 在C中,内存管理是不可避免的一门必修课。C对内存的自由度使其获得了更高的…

单片机的boot升级和双备份升级

同时boot升级还会有一个策略来防止单片机变成砖:就是boot的升级程序写在boot中,这个部分的的升级程序是不会改动的,如果检测到升级失败,会一直等待,直到升级正确的程序

Sora: 大型视觉模型背景、技术、局限性和机遇的综述

论文链接:https://arxiv.org/pdf/2402.17177.pdf 背景 在分析 Sora 之前,研究者首先盘点了视觉内容生成技术的沿袭。 在深度学习革命之前,传统的图像生成技术依赖于基于手工创建特征的纹理合成和纹理映射等方法。这些方法在生成复杂而生动…

全自动气象站的工作原理

TH-CQX5全自动气象站是一款野外高精度监测气象数据的环境气象站设备。它结合了先进的传感器技术、自动化控制系统和远程通信技术,能够提供准确、实时的气象数据,为环境保护、气象研究、农业生产等领域提供重要的数据支持。 高精度传感器:全自…

苹果电脑安装Android Studio和配置SDK

大家好,我是你们的好朋友咕噜铁蛋!今天,我们要来聊一聊关于《苹果电脑安装Android Studio和配置SDK》这个话题。对于使用苹果电脑的开发者来说,安装Android Studio并配置SDK可能会有些不同,但只要跟着我的指引&#xf…

2024-3-5 python 序列小知识点

1、for循环的变量作用域不限于for循环内 >>>i 10 >>>for i in range(100): >>> print(i) >>> i 100此处,for循环里的 i 修改了之前的 i 变量的值。 2、列表推导式里的变量作用域仅限于推导式内 推导式犹如一个函数&…

五、布局布线约束、系统优化参数、时序优化收敛 关键技术点

在实际的工程当中,出现了时序违例的情况如何解决呢? 本章内容将介绍例外约束、布局布线的具体操作,实现系统参数的优化。 **前言:**通过约束时钟,比如基准时钟,和生成时钟,让我们的综合工具知道我们的时序…

嵌入式系统是什么?Linux应用开发是开发什么的?

第一篇: 原文链接:https://www.zhihu.com/question/464205608/answer/3358027187 一、什么是嵌入式系统 嵌入式系统是以应用为中心,以计算机技术为基础,软硬件可裁剪,适用于应用系统,对功能、可靠性、…

Redis之事务(详细解析)

请直接看原文:不能回滚的Redis事务还能用吗 - 知乎 (zhihu.com) ------------------------------------------------------------------------------------------------------------------------------ 1、Redis事务的概念: Redis 事务的本质是一组命令的集合。…

Linux编程3.3 进程-进程的终止

1、正常终止 从main函数返回调用exit(标准C库函数)调用_exti或_Exit(系统调用)最后一个线程从其启动例程返回最后一个线程调用 pthread exit 2、异常终止 调用abort接受到一个信号并终止最后一个线程对取消请求做处理响应 3、进程返回 通常程序运行…

电子邮件营销的优势包括?邮件营销的利弊?

电子邮件营销的优势及特点?电子邮件营销策略的好处? 电子邮件营销不仅是一种经济高效的营销方式,更是企业与潜在客户之间建立长期关系的桥梁。那么,电子邮件营销的优势具体有哪些呢?接下来,让蜂邮EDM来探讨…

【Web安全靶场】upload-labs-master 1-21

upload-labs-master 其他靶场见专栏… 文章目录 upload-labs-masterPass-01-js前端校验Pass-02-MIME校验Pass-03-其他后缀绕过黑名单Pass-04-.hatccess绕过Pass-05-点空格点代码逻辑绕过Pass-06-大小写绕过Pass-07-空格绕过Pass-08-点号绕过Pass-09-::$DATA绕过Pass-10-点空格…

【Redis】Redis持久化模式AOF

目录 引言 AOF持久化模式​编辑​编辑 AOF与RDB的混合持久化(4.x后的新特性) AOF的优缺点 修复破损aof文件 到底用RDB还是AOF 引言 AOF就相当于上面的日志形式。是追加式备份。所有发生的写操作,新增啊,修改啊,删除啊,这些命…

pdf电子准考证查询下载系统(实证效果可照片)V1.0

CSDNpdf电子准考证查询下载系统(实证效果可照片)V1.0 使用场景: 教育机构比如学校用pdf准考证查询下载系统(实证效果可照片,最适合准考证打印); 也可自定义图片及坐标用于各种优秀党员三好学生等荣誉证书、聘书授权代理pdf电子证书查询与下载。 推荐Linux PHP5.5-7.3使用使…