Python内置函数(52)——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, otherwiseAttributeErroris 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的更多相关文章
- Python内置函数(25)——getattr
英文文档: getattr(object, name[, default]) Return the value of the named attribute of object. name must ...
- Python内置函数(52)——range
英文文档: range(stop) range(start, stop[, step]) Rather than being a function, range is actually an immu ...
- Python | 内置函数(BIF)
Python内置函数 | V3.9.1 | 共计155个 还没学完, 还没记录完, 不知道自己能不能坚持记录下去 1.ArithmeticError 2.AssertionError 3.Attrib ...
- Python 内置函数笔记
其中有几个方法没怎么用过, 所以没整理到 Python内置函数 abs(a) 返回a的绝对值.该参数可以是整数或浮点数.如果参数是一个复数,则返回其大小 all(a) 如果元组.列表里面的所有元素都非 ...
- python内置函数大全(分类)
python内置函数大全 python内建函数 最近一直在看python的document,打算在基础方面重点看一下python的keyword.Build-in Function.Build-in ...
- python内置函数和魔法函数
内置方法:Python中声明每一个类系统都会加上一些默认内置方法,提供给系统调用该类的对象时使用.比如需要实例化一个对象时,需要调用该类的init方法:使用print去打印一个类时,其实调用的是str ...
- 【Python】Python内置函数dir详解
1.命令介绍 最近学习并使用了一个python的内置函数dir,首先help一下: 复制代码代码如下: >>> help(dir)Help on built-in function ...
- Python内置函数7
Python内置函数7 1.propertypython内置的一个装饰器可参考https://blog.csdn.net/u013205877/article/details/77804137 2.q ...
- Python内置函数5
Python内置函数5 1.format参考前面字符串方法中的format 2.frozenset([iterable]) iterable -- 可迭代的对象,比如列表.字典.元组等等 返回一个冻结 ...
随机推荐
- Hello English Again
Currently, I just want to write something in English.Maybe I just want to review my Egnlish knowledg ...
- 设计模式——原型模式(C++实现)
#include <iostream> #include <string> using namespace std; class CPrototype { public: CP ...
- python 全栈开发,Day3
python之集合,深浅copy 一.集合 集合是无序的,不重复的数据集合,它里面的元素是可哈希的(不可变类型),但是集合本身是不可哈希(所以集合做不了字典的键)的.以下是集合最重要的两点: 去重,把 ...
- CucumberJS 资源
https://cucumber.io/docs/reference/javascript https://github.com/cucumber/cucumber-js
- 【转载】Perl中的引用
为什么使用引用? 在perl4中,hash表中的value字段只能是scalar,而不能是list,这对于有些情况是很不方便的,比如有下面的数据: Chicago, USAFrankfurt, Ger ...
- python变量定义和定义规范
变量定义的规则: 变量名只能是 字母.数字或下划线的任意组合 变量名的第一个字符不能是数字 以下关键字不能声明为变量名['and', 'as', 'assert', 'break', 'class', ...
- 复习ACCESS注入
0x00前言:在学校看完了ACCESS注入.但当时并没有电脑,所以做好了笔记 回到家自己搭建了一个有ACCESS注入的站进行练习,虽然这可能没有什么用处 毕竟现在大多的网站都有waf或安全狗.而且AC ...
- Mycat 分片规则详解--日期范围 hash 分片
实现方式:其思想和范围取模分片一样,由于日期取模会出现数据热点问题,所以先根据日期分组,再根据时间 hash 使得短期数据分布跟均匀. 优点:避免扩容时的数据迁移,可以在一定程度上避免范围分片的热点问 ...
- 关于css选择器中有小数点的标签获取
需求说明 因为项目中章节配置的时候有小数点,1,1.1,1.2,1.11的标题,这个时候每一行标题的id,class设置成标题号是独一无二的标记.但是,直接用js获取是获取不到的,例如$('#3.22 ...
- jmeter常见问题汇总
Aggregate Report 是 JMeter 常用的一个 Listener,中文被翻译为"聚合报告".今天再次有同行问到这个报告中的各项数据表示什么意思,顺便在这里公布一下, ...