怎么判断 Python 对象是否包含某个属性?

怎么在使用前判断 Python 对象是否包含某个属性或者某个方法?

class FooClass:
    pass

k = FooClass()
k.someAtt = 2
k.att

输出:

Traceback (most recent call last):
  File "Untitled.py", line 6, in <module>
    k.att
AttributeError: FooClass instance has no attribute 'att'
查看回复