英文文档:

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。
#定义类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的更多相关文章

  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. 1、Linux文件结构介绍

    文件目录介绍 bin 可执行的命令 boot 启动相关的程序:boot→grub→grub.conf timeout修改启动时间 dev 设备.硬件相关信息 etc 程序.系统配置文件 home 用户 ...

  2. springboot引用dubbo的方式

    1.以此种接口依赖的方式 这种相当于将其他模块的服务先拿过来dubbo自己在本模块中进行注入,此时可以直接用Spring的@Autowired注解引用 ,这种情况下本模块的扫描dubbo是所引用的模块 ...

  3. 车道线识别之 tusimple 数据集介绍

    Tusimple 是一家做自动驾驶的公司,他也公布了一些其在自动驾驶领域积累的数据,其中有一些是和车道线检测相关的.2018年6 月份,其举办了一次以摄像头图像数据做车道检测的比赛,公开了一部分数据及 ...

  4. 计蒜客 方程的解数 dfs

    题目: https://www.jisuanke.com/course/2291/182237 思路: 来自:https://blog.csdn.net/qq_29980371/article/det ...

  5. selenium 不同版本Driver

    selenium进行UI自动化测试需要Driver支持,不同的浏览器需要不同的Driver,之前使用的Driver可以正常运行,但是总会报一些莫名的问题,经过查找,原来IE的Driver需要与sele ...

  6. Html5+CSS3之音视频播放

    一.视频播放控制 1.Html5支持的视频格式有.webm,.ogg,在Html5中播放视频所用的标签为<video controls="controls" src=&quo ...

  7. Django网站制作根目录,巧用404,可访问根目录任意网页

    原文链接:http://www.bianbingdang.com/article_detail/106.html 在制作网页过程中,网站需要格式各样的验证.比如百度站长.搜狗联盟的校验网站.不止如此, ...

  8. c#提交事务的两种方法

    1. using (TransactionScope ts = new TransactionScope()) { 除非显示调用ts.Complete()方法.否则,系统不会自动提交这个事务.如果在代 ...

  9. P2V后,VMWare ESX 上RedHat AS5网络不通问题的解决办法

    现象: 机器在启动eth0后,可以ping通eth0的IP,但是很快就无法访问了. 原因: red hat 5.x 默认系统安装完成后为xen内核,那么xen内核引导启动后就会有虚拟网卡(vethx. ...

  10. mysql学习2

    1.视图 视图是一个虚拟表(非真实存在),其本质是[根据sql语句获取动态的数据集,并为其命名],用户使用时只需要使用[名称]即可获取结果集,并可以将其当作表来使用. 搜索临时表 SELECT * F ...