📅  最后修改于: 2020-09-20 04:07:35             🧑  作者: Mango
hasattr()
方法的语法为:
hasattr(object, name)
getattr()调用hasattr()
以检查是否引发AttributeError
。
hasattr()
方法采用两个参数:
hasattr()
方法返回:
class Person:
age = 23
name = 'Adam'
person = Person()
print('Person has age?:', hasattr(person, 'age'))
print('Person has salary?:', hasattr(person, 'salary'))
输出
Person has age?: True
Person has salary?: False