hasattr()vs try-except块来处理不存在的属性
发布于 2021-01-29 18:42:06
if hasattr(obj, 'attribute'):
# do somthing
与
try:
# access obj.attribute
except AttributeError, e:
# deal with AttributeError
应该首选哪个,为什么?
关注者
0
被浏览
40