当前位置:网站首页>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.
边栏推荐
猜你喜欢
随机推荐
OD-Model【4】:SSD
Redis笔记进阶篇:万字长文-整理Redis,各种知识点,建议收藏
MySQL中JOIN的用法
「PHP基础知识」空值(null)的使用
(转帖)HashCode总结(2)
subprocess.CalledProcessError: Command ‘pip install ‘thop‘‘ returned non-zero exit status 1.
二维数组实战项目--------《三子棋》
[Remote Control Development Basic Tutorial 3] Crazy Shell Open Source Formation UAV-ADC (Joystick Control)
LeetCode:1374. 生成每种字符都是奇数个的字符串【签到题】
PCL—点云数据分割
CV-Model【4】:MobileNet v3
JVM学习----垃圾回收--G1
Small program (necessary common sense for development) 1
线性代数学习笔记2-2:向量空间、子空间、最大无关组、基、秩与空间维数
oracle内连接和外连接
2022.7.30 js笔记 运算符和流程控制符、循环
MySql创建数据表
mysql中json类型字段用法
知识工程作业2:知识工程相关领域介绍
分布式领域最重要的一篇论文,到底讲了什么?