hasattr()函数在Python中做什么?
Python中的hasattr()方法
如果对象具有给定的命名属性,则hasattr()方法返回true,否则返回false。
语法
hasattr()方法的语法是-
hasattr(object, name)
getattr()调用hasattr()以检查是否引发AttributeError。
hasattr()方法采用两个参数-
hasattr()方法返回-
如果对象具有给定的命名属性,则为true
假,如果对象没有给定的命名属性
示例
class Male:
age = 21
name = 'x'
x = Male()
print('Male has age?:', hasattr(x, 'age'))
print('Male has salary?:', hasattr(x, 'salary'))输出结果
这给出了输出
('Male has age?:', True)
('Male has salary?:', False)热门推荐
10 订亲求婚蛋糕祝福语简短
11 上级转岗祝福语大全简短
12 婚礼时祝福语简短霸气
13 退出支部的祝福语简短
14 恭祝新婚祝福语简短高级
15 新婚贺卡老师祝福语简短
16 给英雄的祝福语简短
17 入住酒店文案祝福语简短
18 新婚出门老人祝福语简短