getattr()从名字上看获取属性值,若属性存在,返回属性值,若属性不存在,则报错,具体的用法如下:
class Person():
age = 14
Tom = Person()
print(getattr(Tom,'age'))#14
print(getattr(Tom,'name'))#因为name属性不存在,这里会报错AttributeError: 'Person' object has no attribute 'name'
getattr()从名字上看获取属性值,若属性存在,返回属性值,若属性不存在,则报错,具体的用法如下:
class Person():
age = 14
Tom = Person()
print(getattr(Tom,'age'))#14
print(getattr(Tom,'name'))#因为name属性不存在,这里会报错AttributeError: 'Person' object has no attribute 'name'