当前位置:网站首页>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 12 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.
边栏推荐
猜你喜欢

Scaffolding installation

Redis安装,基本命令,持久化方式,集群

LeetCode:746. 使用最小花费爬楼梯【动态规划】

线性代数学习笔记3-1:矩阵与线性变换、常见矩阵(逆矩阵、伴随矩阵、正交矩阵等)
![[Basic Tutorial of Remote Control Development 1] Crazy Shell Open Source Formation Drone-GPIO (Remote Control Indicator Light Control)](/img/da/5f6b1ed89cb3fed3cbe6aea840f473.png)
[Basic Tutorial of Remote Control Development 1] Crazy Shell Open Source Formation Drone-GPIO (Remote Control Indicator Light Control)

redis进行持久化时,有新的写操作将如何解决——写时复制

Mysql8创建用户以及赋权操作

磷脂-聚乙二醇-醛基 DSPE-PEG-Aldehyde DSPE-PEG-CHO MW:5000

活体检测 Adaptive Normalized Representation Learning for GeneralizableFace Anti-Spoofing 阅读笔记

黑马案例--实现 clock 时钟的web服务器
随机推荐
oracle内连接和外连接
我的两周年创作纪念日
oracle inner join and outer join
Freeswitch操作基本配置
MySQL分组后取最大一条数据【最优解】
JJWT工具类
MySQL两阶段提交串讲
DSPE-PEG-PDP,DSPE-PEG-OPSS,磷脂-聚乙二醇-巯基吡啶供应,MW:5000
API 低代码开发:接口大师,一套开发、管理和提供接口的产品框架
Error: with open(txt_path,'r') as f: FileNotFoundError: [Errno 2] No such file or directory:
线性代数学习笔记1:何为线性代数
【面经】米哈游数据开发一面二面面经
OD-Model【4】:SSD
Keil development environment installation tutorial
debian 10 nat 与路由转发
Flexbox flex property
Small program (necessary common sense for development) 1
腾讯50题
基本运算符
HCIP-第十一天-MPLS+BGP