Python内置函数(25)——getattr
英文文档:
getattr
(object, name[, default])- Return the value of the named attribute of object. name must be a string. If the string is the name of one of the object’s attributes, the result is the value of that attribute. For example,
getattr(x, 'foobar')
is equivalent tox.foobar
. If the named attribute does not exist, default is returned if provided, otherwiseAttributeError
is raised. - 说明:
- 1. 函数功能是从对象object中获取名称为name的属性,等效与调用object.name。
- #定义类Student
- >>> class Student:
- def __init__(self,name):
- self.name = name
- >>> s = Stduent('Aim')
- >>> getattr(s,'name') #等效于调用s.name
- 'Aim'
- >>> s.name
- 'Aim'
2. 函数第三个参数default为可选参数,如果object中含义name属性,则返回name属性的值,如果没有name属性,则返回default值,如果default未传入值,则报错。
- #定义类Student
- >>> class Student:
- def __init__(self,name):
- self.name = name
- >>> getattr(s,'name') #存在属性name
- 'Aim'
- >>> getattr(s,'age',6) #不存在属性age,但提供了默认值,返回默认值
- 6
- >>> getattr(s,'age') #不存在属性age,未提供默认值,调用报错
- Traceback (most recent call last):
- File "<pyshell#17>", line 1, in <module>
- getattr(s,'age')
- AttributeError: 'Stduent' object has no attribute 'age'
Python内置函数(25)——getattr的更多相关文章
- Python内置函数(52)——getattr
英文文档: getattr(object, name[, default]) Return the value of the named attribute of object. name must ...
- Python内置函数(25)——frozenset
英文文档: class frozenset([iterable]) Return a new frozenset object, optionally with elements taken from ...
- Python内置函数7
Python内置函数7 1.propertypython内置的一个装饰器可参考https://blog.csdn.net/u013205877/article/details/77804137 2.q ...
- Python内置函数和内置常量
Python内置函数 1.abs(x) 返回一个数的绝对值.实参可以是整数或浮点数.如果实参是一个复数,返回它的模. 2.all(iterable) 如果 iterable 的所有元素为真(或迭代器为 ...
- Python | 内置函数(BIF)
Python内置函数 | V3.9.1 | 共计155个 还没学完, 还没记录完, 不知道自己能不能坚持记录下去 1.ArithmeticError 2.AssertionError 3.Attrib ...
- Python基础篇【第2篇】: Python内置函数(一)
Python内置函数 lambda lambda表达式相当于函数体为单个return语句的普通函数的匿名函数.请注意,lambda语法并没有使用return关键字.开发者可以在任何可以使用函数引用的位 ...
- Python 内置函数笔记
其中有几个方法没怎么用过, 所以没整理到 Python内置函数 abs(a) 返回a的绝对值.该参数可以是整数或浮点数.如果参数是一个复数,则返回其大小 all(a) 如果元组.列表里面的所有元素都非 ...
- 【转】python 内置函数总结(大部分)
[转]python 内置函数总结(大部分) python 内置函数大讲堂 python全栈开发,内置函数 1. 内置函数 python的内置函数截止到python版本3.6.2,现在python一共为 ...
- python内置函数,匿名函数
一.匿名函数 匿名函数:为了解决那些功能很简单的需求而设计的一句话函数 def calc(n): return n**n print(calc(10)) #换成匿名函数 calc = lambda n ...
随机推荐
- having使用
啰嗦点: where子句用来筛选 from子句中指定的操作所产生的行. group by 子句用来分组 where子句的输出. having子句用来从分组的结果中筛选行. 对于可以在分组操作之前或之后 ...
- Set接口下的集合
一.集合(定义字段的时候统一使用包装类) 1.集合大类分为List.Set.Map三种,其中,List集合是有序可重复的,并且可以使用普通for循环.增强for循环.正向迭代器.双向迭代器:Set集合 ...
- Android Studio之回退Gradle版本方法
Android Studio之回退Gradle版本方法 (Minimum supported Gradle version is 4.10.1. Current version is 4.6.) ...
- Unittest框架+ddt数据驱动+HTMLTestRunner+sendmail(自动发送测试报告)+git+Jenkins
本次写的是针对有代码基础的,没基础建议先去学基础,以下所有描述内容都是我已经在公司项目实践成功的!仅供参考 整体思路: 1.接口自动化用的是Python中unittest框架 2.所有的测试数据用例存 ...
- 启动nginx报错问题
为了解决生产环境的bug,模拟生产环境,我使用了nginx,在安装启动的过程中,出现了很多问题. 1.nginx下载地址 http://nginx.org/en/download.html 这是ngi ...
- 雕刻机制作 PCB 指南
之前使用过感光蓝油制作过 PCB,虽然感光法精度高,但个人制作耗时耗力,发给厂家周期又很长.看到国外的网友使用雕刻机制作 PCB 视频之后.几番周折之后还是成功了.有感于网上几乎没有一份完整的雕刻机 ...
- MongoDB 学习使用
博客教程: https://jingyan.baidu.com/article/dca1fa6f0428a4f1a440522e.html
- mysql import error
mysql导入文件一直出错,显示ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option s ...
- 旧项目Makefile 迁移CMake的一种方法:include Makefile
有些c++旧项目用Makefile,要迁移CMake的时候非常痛苦,有些像static pattern的语法和make自带命令 cmake要重写一套非常的麻烦. 因此这里用trick的方法实现了一种i ...
- web 12
调用一个地图(百度地图)API(定位) 到网站: 1.调用API的js : <script type="text/javascript" src="https:// ...