英文文档:

getattr(objectname[, default])
Return the value of the named attribute of objectname 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'
与__getattr__的区别:
  • __getattr__是类的内置方法,当找不到某个属性时会调用该方法;找到就不会调用.
  • getattr与类无关.
  • 一个例子:作为data的代理类,可以以这种方式来使用data的属性.
  • class DataProxy(...):
    
        def __getattr__(self, item):
    return getattr(self.data, item)

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

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

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

  2. Python内置函数(52)——range

    英文文档: range(stop) range(start, stop[, step]) Rather than being a function, range is actually an immu ...

  3. Python | 内置函数(BIF)

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

  4. Python 内置函数笔记

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

  5. python内置函数大全(分类)

    python内置函数大全 python内建函数 最近一直在看python的document,打算在基础方面重点看一下python的keyword.Build-in Function.Build-in ...

  6. python内置函数和魔法函数

    内置方法:Python中声明每一个类系统都会加上一些默认内置方法,提供给系统调用该类的对象时使用.比如需要实例化一个对象时,需要调用该类的init方法:使用print去打印一个类时,其实调用的是str ...

  7. 【Python】Python内置函数dir详解

    1.命令介绍 最近学习并使用了一个python的内置函数dir,首先help一下: 复制代码代码如下: >>> help(dir)Help on built-in function ...

  8. Python内置函数7

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

  9. Python内置函数5

    Python内置函数5 1.format参考前面字符串方法中的format 2.frozenset([iterable]) iterable -- 可迭代的对象,比如列表.字典.元组等等 返回一个冻结 ...

随机推荐

  1. div闪一下就消失

    <div id="saveCode" class="center" style="display:none;width:300px;height ...

  2. 为eclipse安装subclipse(SVN插件)

    1.打开eclipse,点击菜单Help->Install New Software 2.进入安装窗体后,点击安装窗体的右侧的Add按钮,然后在弹出的窗体中输入名称:Subclipse 1.12 ...

  3. python全栈开发-Day4 列表

    python全栈开发-Day4 列表 一.首先按照以下几个点展开列表的学习 #一:基本使用 1 用途 2 定义方式 3 常用操作+内置的方法 #二:该类型总结 1 存一个值or存多个值 只能存一个值 ...

  4. SSM 使用 mybatis 分页插件 pagehepler 实现分页

    使用分页插件的原因,简化了sql代码的写法,实现较好的物理分页,比写一段完整的分页sql代码,也能减少了误差性. Mybatis分页插件 demo 项目地址:https://gitee.com/fre ...

  5. 理解 dispatch_get_specific

    这篇文章原来在用 Github Pages 搭建的博客上,现在决定重新用回博客园,所以把文章搬回来. dispatch_queue_set_specific用于给一个队列设置相关的上下文数据,disp ...

  6. Web开发中Listener、Filter、Servlet的初始化及调用

    我们在使用Spring+SpringMVC开发项目中,web.xml中一般的配置如下: <?xml version="1.0" encoding="UTF-8&qu ...

  7. springboot集成swagger2

    介绍:        Swagger是全球最大的OpenAPI规范(OAS)API开发工具框架,支持从设计和文档到测试和部署的整个API生命周期的开发.(摘自Swagger官网)Swagger说白了就 ...

  8. from提交表单后 数据提交到后台 但不跳转页面 可用iframe

    可以页面事先加载被隐藏的iframe标签,或者等到需要的时候通过js生成,再提交,提交之前,form的target指向iframe(我是要实现新页面生成的时候程半透明状态,所以用了后者的方法) 代码如 ...

  9. ng-if ng-show ng-hide区别(面试题)

    ng-if ng-show  ng-hide区别 实现原理方面: ng-show/ng-hide是通过修改css样式方式控制元素显示与隐藏,对应的DOM元素会一直存在于当前页面中: 而ng-if根据表 ...

  10. 【Linux】 CentOS7 虚拟机配置

    Linux虚拟机配置 从去年开始实习开始,公司电脑换了两个,自己的电脑也换了一个,每换一个新电脑,总免不了要去装一个Linux的虚拟机作为试验用.但是每次新装一个机器总是会遇到各种各样的问题让我用的不 ...