Python进阶——自省
自省——指在运行时判断一个对象的类型的能力。
一、 dir
dir是自省的最重要的函数之一。
它返回一个个列表,列出了一个对象所拥有的属性和方法。
例子:
my_list={1,2}
dir(my_list)
输出:
['__and__','__class__','__contains__','__delattr__','__dir__','__doc__','__eq__','__format__','__ge__','__getattribute__','__gt__','__hash__','__iand__','__init__','__init_subclass__','__ior__','__isub__','__iter__','__ixor__','__le__','__len__','__lt__','__ne__','__new__','__or__','__rand__','__reduce__','__reduce_ex__','__repr__','__ror__','__rsub__','__rxor__','__setattr__','__sizeof__','__str__','__sub__','__subclasshook__','__xor__','add','clear','copy','difference','difference_update','discard','intersection','intersection_update','isdisjoint','issubset','issuperset','pop','remove','symmetric_difference','symmetric_difference_update','union','update']
上面的自省给了我们该列表对象所有的方法的名字。如果运行dir()不传入参数,则会返回当前作用域所有名字。
二、type和id
type函数返回一个对象的类型。如:
print(type(""))
print(type([]))
print(type({}))
输出:
<class 'str'>
<class 'list'>
<class 'dict'>
id()函数返回任意不同种类对象的唯一ID,如:
name="hello"
print(id(name))
输出:
51702304
三、inspect模块
该模块提供了许多有用的函数,来获取活跃对象的信息。比如:
查看一个对象的成员:
import inspect
print(inspect.getmembers(str))
输出:
[('__add__', <slot wrapper '__add__' of 'str' objects>), ('__class__', <class 'type'>), ('__contains__', <slot wrapper '__contains__' of 'str' objects>), ('__delattr__', <slot wrapper '__delattr__' of 'object' objects>), ('__dir__', <method '__dir__' of 'object' objects>), ('__doc__', "str(object='') -> str\nstr(bytes_or_buffer[, encoding[, errors]]) -> str\n\nCreate a new string object from the given object. If encoding or\nerrors is specified, then the object must expose a data buffer\nthat will be decoded using the given encoding and error handler.\nOtherwise, returns the result of object.__str__() (if defined)\nor repr(object).\nencoding defaults to sys.getdefaultencoding().\nerrors defaults to 'strict'."), ('__eq__', <slot wrapper '__eq__' of 'str' objects>), ('__format__', <method '__format__' of 'str' objects>), ('__ge__', <slot wrapper '__ge__' of 'str' objects>), ('__getattribute__', <slot wrapper '__getattribute__' of 'str' objects>), ('__getitem__', <slot wrapper '__getitem__' of 'str' objects>), ('__getnewargs__', <method '__getnewargs__' of 'str' objects>), ('__gt__', <slot wrapper '__gt__' of 'str' objects>), ('__hash__', <slot wrapper '__hash__' of 'str' objects>), ('__init__', <slot wrapper '__init__' of 'object' objects>), ('__init_subclass__', <built-in method __init_subclass__ of type object at 0x6486BF00>), ('__iter__', <slot wrapper '__iter__' of 'str' objects>), ('__le__', <slot wrapper '__le__' of 'str' objects>), ('__len__', <slot wrapper '__len__' of 'str' objects>), ('__lt__', <slot wrapper '__lt__' of 'str' objects>), ('__mod__', <slot wrapper '__mod__' of 'str' objects>), ('__mul__', <slot wrapper '__mul__' of 'str' objects>), ('__ne__', <slot wrapper '__ne__' of 'str' objects>), ('__new__', <built-in method __new__ of type object at 0x6486BF00>), ('__reduce__', <method '__reduce__' of 'object' objects>), ('__reduce_ex__', <method '__reduce_ex__' of 'object' objects>), ('__repr__', <slot wrapper '__repr__' of 'str' objects>), ('__rmod__', <slot wrapper '__rmod__' of 'str' objects>), ('__rmul__', <slot wrapper '__rmul__' of 'str' objects>), ('__setattr__', <slot wrapper '__setattr__' of 'object' objects>), ('__sizeof__', <method '__sizeof__' of 'str' objects>), ('__str__', <slot wrapper '__str__' of 'str' objects>), ('__subclasshook__', <built-in method __subclasshook__ of type object at 0x6486BF00>), ('capitalize', <method 'capitalize' of 'str' objects>), ('casefold', <method 'casefold' of 'str' objects>), ('center', <method 'center' of 'str' objects>), ('count', <method 'count' of 'str' objects>), ('encode', <method 'encode' of 'str' objects>), ('endswith', <method 'endswith' of 'str' objects>), ('expandtabs', <method 'expandtabs' of 'str' objects>), ('find', <method 'find' of 'str' objects>), ('format', <method 'format' of 'str' objects>), ('format_map', <method 'format_map' of 'str' objects>), ('index', <method 'index' of 'str' objects>), ('isalnum', <method 'isalnum' of 'str' objects>), ('isalpha', <method 'isalpha' of 'str' objects>), ('isascii', <method 'isascii' of 'str' objects>), ('isdecimal', <method 'isdecimal' of 'str' objects>), ('isdigit', <method 'isdigit' of 'str' objects>), ('isidentifier', <method 'isidentifier' of 'str' objects>), ('islower', <method 'islower' of 'str' objects>), ('isnumeric', <method 'isnumeric' of 'str' objects>), ('isprintable', <method 'isprintable' of 'str' objects>), ('isspace', <method 'isspace' of 'str' objects>), ('istitle', <method 'istitle' of 'str' objects>), ('isupper', <method 'isupper' of 'str' objects>), ('join', <method 'join' of 'str' objects>), ('ljust', <method 'ljust' of 'str' objects>), ('lower', <method 'lower' of 'str' objects>), ('lstrip', <method 'lstrip' of 'str' objects>), ('maketrans', <built-in method maketrans of type object at 0x6486BF00>), ('partition', <method 'partition' of 'str' objects>), ('replace', <method 'replace' of 'str' objects>), ('rfind', <method 'rfind' of 'str' objects>), ('rindex', <method 'rindex' of 'str' objects>), ('rjust', <method 'rjust' of 'str' objects>), ('rpartition', <method 'rpartition' of 'str' objects>), ('rsplit', <method 'rsplit' of 'str' objects>), ('rstrip', <method 'rstrip' of 'str' objects>), ('split', <method 'split' of 'str' objects>), ('splitlines', <method 'splitlines' of 'str' objects>), ('startswith', <method 'startswith' of 'str' objects>), ('strip', <method 'strip' of 'str' objects>), ('swapcase', <method 'swapcase' of 'str' objects>), ('title', <method 'title' of 'str' objects>), ('translate', <method 'translate' of 'str' objects>), ('upper', <method 'upper' of 'str' objects>), ('zfill', <method 'zfill' of 'str' objects>)]
Python进阶——自省相关推荐
- python进阶教程
Python进阶中文教程 转自GitBookhttps://eastlakeside.gitbooks.io/interpy-zh/content/?q= 我仔细学习了该进阶教程,原著根据<In ...
- Python进阶之递归函数的用法及其示例
作者 | 程序员adny 责编 | 徐威龙 封图| CSDN│下载于视觉中国 出品 | AI科技大本营(ID:rgznai100) 本篇文章主要介绍了Python进阶之递归函数的用法及其示例,现在分 ...
- Python自学路线图之Python进阶
Python自学路线图的第二个阶段是Python进阶学习,自学完后需要掌握的Python技能: 1.自学Linux操作系统,熟练使用Linux操作系统: 自学网络编程,掌握网络编程相关技术, 能够实现 ...
- 如果只推荐一本 Python 进阶的书,我要 Pick 它!
作者 | 豌豆花下猫 今年二月初,我偶然看到了一条推特: <流畅的Python>一书的作者发布了一条激动人心的消息:他正在写作第二版! 如果要票选最佳的 Python 进阶类书目,这本书肯 ...
- 106页的《Python进阶》中文版(附下载)!
点击上方"AI遇见机器学习",选择"星标"公众号 重磅干货,第一时间送 推荐一本对Python感兴趣的书籍<Python进阶>,是<Inter ...
- Python进阶6——序列操作
1.序列的拼接和复制 Python中使用+对序列进行拼接,使用*对序列进行复制 s=str(1234) l=list(range(2,13)) print(s,l) print('---------- ...
- Python 进阶之路 (九) 再立Flag, 社区最全的itertools深度解析(上)
前言 大家好,今天想和大家分享一下我的itertools学习体验及心得,itertools是一个Python的自带库,内含多种非常实用的方法,我简单学习了一下,发现可以大大提升工作效率,在sf社区内没 ...
- Python 进阶_生成器 生成器表达式
目录 目录 相关知识点 生成器 生成器 fab 的执行过程 生成器和迭代器的区别 生成器的优势 加强的生成器特性 生成器表达式 生成器表达式样例 小结 相关知识点 Python 进阶_迭代器 & ...
- python进阶书籍推荐-豆瓣评分9.4!年度最值得推荐的Python进阶书
原标题:豆瓣评分9.4!年度最值得推荐的Python进阶书 来自:程序员书库(ID:OpenSourceTop) 编译 链接:https://whatpixel.com/fluent-python-b ...
- Python 进阶之路 (十二) 尾声即是开始
Python进阶之路总结 大家好,我的<< Python进阶之路>>到这一期就到此为止了,和 <<Python 基础起步>>不同,在掌握了一些基础知识后 ...
最新文章
- SpringBoot学习之路:09.Spring Boot中添加Filter应用
- maven配置阿里云仓库镜像
- python快速编程入门第13章-Python快速编程入门,打牢基础必须知道的11个知识点...
- Python 字典中get() 函数
- android手机抓包工具需root,android7及以上版本手机抓包
- java使用itext编辑pdf,动态生成pdf文件(从利用Adobe创建pdf模板开始一步步详细介绍)
- 计算机sci论文怎么写,计算机学院陈端兵教授分享SCI论文写作方法
- java属于什么语言_java是什么语言 ?是什么系统?
- 论文第一部分写作总结(introduction)
- STM32+IAP方案 实现网络升级应用固件
- 使用Python获取DNS记录
- php语句执行顺序,sql语句执行顺序是什么
- apache poi-检测到Zip Bomb解决方案
- 2021年富平迤山中学高考成绩查询,本地:迤山中学高考成绩喜人
- can总线配置读入是什么意思_CAN总线含义
- ad19怎么手动布线_Altium Designer手动布线的最佳设置
- java正则匹配汉字_正则表达式匹配中文汉字
- 黑客马拉松比赛:前浪、后浪都是一个浪!
- 一般人我不告诉他的15种App推广流氓手段(上)
- python count函数代码_python count函数用法详解_后端开发
热门文章
- java实现界面化,java实现图形化界面
- Super Iservice 发布地图三维服务
- 神鬼传奇客户端解包图片(ui\common)
- 串口调试助手fx2n_PLC串口调试助手
- PIXHAWK飞行模式
- 读取xls格式的文件
- python长度单位转化_所有长度单位的换算
- C盘爆满,使用DiskGenius调整C盘大小,实操记录
- 华为鸿蒙系统发布会时间,华为“跑步”进场,鸿蒙发布时间确定,幸福来得太突然...
- 你吃的面粉可能是死老鼠和姨妈巾的混合物