当前位置:网站首页>getattr() function analysis
getattr() function analysis
2022-08-02 03:33:00 【woshicaiji12138】
1Brief
getattr(object, name [, default ])
object is an object, and name must be a string; if the value of name is an attribute of the object object, the function outputs the attribute of the object, which is equivalent to object.name.
class Student:def __init__(self,name):self.name = names = Stduent('zhangsan')getattr(s,'name')
The output result is zhangsan.
If the value of name is not an attribute of the object object, if the default value is set at this time, the function will output the default value; otherwise, an error will be reported.
class Student:def __init__(self,name):self.name = namegetattr(s,'age',1) #The attribute age does not exist, but a default value is provided, return the default value#will output 1
2 Application
In general, the above functions are multi-purposed with a certain attribute of object. However, for an object with multiple attributes, the getattr() function can be used when the type of the attribute needs to be called at present.When you can realize which property you need, you can type the name to get the corresponding value.
边栏推荐
- [Basic Tutorial of Remote Control Development 1] Crazy Shell Open Source Formation Drone-GPIO (Remote Control Indicator Light Control)
- 云服务器安装部署Nacos2.0.4版本
- 磷脂-聚乙二醇-靶向新生血管靶向肽APRPG,DSPE-PEG-APRPG
- ImportError: libGL.so.1: cannot open shared object file: No such file or directory
- 【程序人生】做了多年的运维,靠什么转行拿下12K+年终奖的薪资?
- Deveco studio 鸿蒙app访问网络详细过程(js)
- @Autowired与@Resource区别
- 知识问答与知识会话的区别
- 【C语言万字长文】 宏定义 结构体 共用体 内存对齐知识点总结
- AttributeError: Can't get attribute 'SPPF' on
猜你喜欢
随机推荐
DSPE-PEG-Silane,DSPE-PEG-SIL,磷脂-聚乙二醇-硅烷修饰活性基团
2022年比若依更香的开源项目
DSPE-PEG-PDP,DSPE-PEG-OPSS,磷脂-聚乙二醇-巯基吡啶供应,MW:5000
parser = argparse.ArgumentParser()解析
@Configuration详解
Flexbox flex property
磷脂-聚乙二醇-酰肼,DSPE-PEG-Hydrazide,DSPE-PEG-HZ,MW:5000
Mysql8.0安装教程
MySQL常见的索引
TRICK第二弹
腾讯50题
Daily practice------There are n integers, so that the previous numbers are moved back m positions in order, and the last m numbers become the first m numbers
连接数据库时遇到的bug1号
Redis简单学习笔记
Scaffolding installation
小程序(开发必备常识)1
【面经】米哈游数据开发一面二面面经
Double Strings (don't always forget substr)
Redis笔记基础篇:6分钟看完Redis的八种数据类型
周日数据库作业