python判断对象是否有属性

判断类对象类型,是否有包含属性: 


class FooClass:
    pass

k = FooClass()
k.append =12
try:
    # 判断属性
    if hasattr(k, 'append'):
        print(k.append)
    if hasattr(k, 'att'):
        print(1)
    else:
        print("has no att")
    print(k.att)
except AttributeError as e:
    # error: has not attribute
    pass


#判断对象类型:

class_name=k.__class__.__name__

if class_name=="FooClass":
    print("class_name ok")

isdf=isinstance(k, FooClass)
print(isdf)

type_a=type(k)

print(type_a==FooClass)

你可能感兴趣的:(python基础,python,开发语言)