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,一经查实,立即删除!

相关文章

面试重点项

最近准备面试了,梳理一些面试经常考试的细节点,不然每次都要去整理,答案未给出。 一、C语言基础 1.1、字节对齐 link Struct和Union字节对齐的内存占用计算方法 link 怎么计算union和struct中字节数计算 1.2、union数据空间大小计算 link 怎么计算u…

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对内存的自由度使其获得了更高的…

教你用Android Studio如何打jar包与aar包

目录 jar与aar区别 生成jar与aar前言 常规来说,我们项目接入第三方的SDK,是有以下两种方法的: 1.构建项目打包出Android原生工程,使用Android Studio在原生工程里面写接入第三方SDK的逻辑代码。 2.使用Android Studio创建工程,在工程里写接入第三方SDK的逻辑代码…

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

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

Mac M2机器使用python3的lxml报错symbol not found in flat namespace

报错详情如下: ...... from lxml import etree ImportError: dlopen(/opt/miniconda3/envs/python38/lib/python3.8/site-packages/lxml/etree.cpython-38-darwin.so, 0x0002): symbol not found in flat namespace _exsltDateXpathCtxtRegister 解决方法&…

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 一、什么是嵌入式系统 嵌入式系统是以应用为中心,以计算机技术为基础,软硬件可裁剪,适用于应用系统,对功能、可靠性、…

Java开发人员应学习的10种工具

1.Docker Docker是一个开源工具,用于自动在云或物理服务器上部署应用程序。它允许开发人员和系统管理员在笔记本电脑,数据中心VM或云上构建,交付和运行分布式应用程序。 Docker与云,Linux和Windows供应商合作,并且许多…

LeetCode 2810.故障键盘

你的笔记本键盘存在故障,每当你在上面输入字符 ‘i’ 时,它会反转你所写的字符串。而输入其他字符则可以正常工作。 给你一个下标从 0 开始的字符串 s ,请你用故障键盘依次输入每个字符。 返回最终笔记本屏幕上输出的字符串。 示例 1&…