英文文档:

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 to x.foobar. If the named attribute does not exist, default is returned if provided, otherwise AttributeError is raised.
说明:
  
  1. 函数功能是从对象object中获取名称为name的属性,等效与调用object.name。
  1. #定义类Student
  2. >>> class Student:
  3. def __init__(self,name):
  4. self.name = name
  5.  
  6. >>> s = Stduent('Aim')
  7. >>> getattr(s,'name') #等效于调用s.name
  8. 'Aim'
  9. >>> s.name
  10. 'Aim'

  2. 函数第三个参数default为可选参数,如果object中含义name属性,则返回name属性的值,如果没有name属性,则返回default值,如果default未传入值,则报错。

  1. #定义类Student
  2. >>> class Student:
  3. def __init__(self,name):
  4. self.name = name
  5.  
  6. >>> getattr(s,'name') #存在属性name
  7. 'Aim'
  8.  
  9. >>> getattr(s,'age',6) #不存在属性age,但提供了默认值,返回默认值
  10. 6
  11.  
  12. >>> getattr(s,'age') #不存在属性age,未提供默认值,调用报错
  13. Traceback (most recent call last):
  14. File "<pyshell#17>", line 1, in <module>
  15. getattr(s,'age')
  16. AttributeError: 'Stduent' object has no attribute 'age'

Python内置函数(25)——getattr的更多相关文章

  1. Python内置函数(52)——getattr

    英文文档: getattr(object, name[, default]) Return the value of the named attribute of object. name must ...

  2. Python内置函数(25)——frozenset

    英文文档: class frozenset([iterable]) Return a new frozenset object, optionally with elements taken from ...

  3. Python内置函数7

    Python内置函数7 1.propertypython内置的一个装饰器可参考https://blog.csdn.net/u013205877/article/details/77804137 2.q ...

  4. Python内置函数和内置常量

    Python内置函数 1.abs(x) 返回一个数的绝对值.实参可以是整数或浮点数.如果实参是一个复数,返回它的模. 2.all(iterable) 如果 iterable 的所有元素为真(或迭代器为 ...

  5. Python | 内置函数(BIF)

    Python内置函数 | V3.9.1 | 共计155个 还没学完, 还没记录完, 不知道自己能不能坚持记录下去 1.ArithmeticError 2.AssertionError 3.Attrib ...

  6. Python基础篇【第2篇】: Python内置函数(一)

    Python内置函数 lambda lambda表达式相当于函数体为单个return语句的普通函数的匿名函数.请注意,lambda语法并没有使用return关键字.开发者可以在任何可以使用函数引用的位 ...

  7. Python 内置函数笔记

    其中有几个方法没怎么用过, 所以没整理到 Python内置函数 abs(a) 返回a的绝对值.该参数可以是整数或浮点数.如果参数是一个复数,则返回其大小 all(a) 如果元组.列表里面的所有元素都非 ...

  8. 【转】python 内置函数总结(大部分)

    [转]python 内置函数总结(大部分) python 内置函数大讲堂 python全栈开发,内置函数 1. 内置函数 python的内置函数截止到python版本3.6.2,现在python一共为 ...

  9. python内置函数,匿名函数

    一.匿名函数 匿名函数:为了解决那些功能很简单的需求而设计的一句话函数 def calc(n): return n**n print(calc(10)) #换成匿名函数 calc = lambda n ...

随机推荐

  1. having使用

    啰嗦点: where子句用来筛选 from子句中指定的操作所产生的行. group by 子句用来分组 where子句的输出. having子句用来从分组的结果中筛选行. 对于可以在分组操作之前或之后 ...

  2. Set接口下的集合

    一.集合(定义字段的时候统一使用包装类) 1.集合大类分为List.Set.Map三种,其中,List集合是有序可重复的,并且可以使用普通for循环.增强for循环.正向迭代器.双向迭代器:Set集合 ...

  3. Android Studio之回退Gradle版本方法

    Android Studio之回退Gradle版本方法 (Minimum supported Gradle version is 4.10.1. Current version is 4.6.)   ...

  4. Unittest框架+ddt数据驱动+HTMLTestRunner+sendmail(自动发送测试报告)+git+Jenkins

    本次写的是针对有代码基础的,没基础建议先去学基础,以下所有描述内容都是我已经在公司项目实践成功的!仅供参考 整体思路: 1.接口自动化用的是Python中unittest框架 2.所有的测试数据用例存 ...

  5. 启动nginx报错问题

    为了解决生产环境的bug,模拟生产环境,我使用了nginx,在安装启动的过程中,出现了很多问题. 1.nginx下载地址 http://nginx.org/en/download.html 这是ngi ...

  6. 雕刻机制作 PCB 指南

    之前使用过感光蓝油制作过 PCB,虽然感光法精度高,但个人制作耗时耗力,发给厂家周期又很长.看到国外的网友使用雕刻机制作 PCB  视频之后.几番周折之后还是成功了.有感于网上几乎没有一份完整的雕刻机 ...

  7. MongoDB 学习使用

    博客教程: https://jingyan.baidu.com/article/dca1fa6f0428a4f1a440522e.html

  8. mysql import error

    mysql导入文件一直出错,显示ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option s ...

  9. 旧项目Makefile 迁移CMake的一种方法:include Makefile

    有些c++旧项目用Makefile,要迁移CMake的时候非常痛苦,有些像static pattern的语法和make自带命令 cmake要重写一套非常的麻烦. 因此这里用trick的方法实现了一种i ...

  10. web 12

    调用一个地图(百度地图)API(定位) 到网站: 1.调用API的js : <script type="text/javascript" src="https:// ...