Tag: hasattr

hasattr()vs try-except块来处理不存在的属性

if hasattr(obj, 'attribute'): # do somthing VS try: # access obj.attribute except AttributeError, e: # deal with AttributeError 哪个应该是首选的,为什么?