python3 hasattr().py

"""
模块:python3 hasattr().py
功能:Python hasattr() 函数
参考:https://www.runoob.com/python/python-func-hasattr.html
知识点:
1.hasattr(obj, name, /) -> True/False
    判断对象是否具有给定名称的属性。
    这是通过调用 getattr(obj,name) 和捕获 AttributeError 错误来完成的。
    obj -- 对象。
    name -- 字符串,属性名。
"""


class Coordinate:
    x = 10
    y = -5
    z = 0


point1 = Coordinate()
print(hasattr(point1, 'x'))  # True
print(hasattr(point1, 'y'))  # True
print(hasattr(point1, 'z'))  # True
print(hasattr(point1, 'no'))  # False
 

发布了197 篇原创文章 · 获赞 61 · 访问量 11万+

猜你喜欢

转载自blog.csdn.net/weixin_42193179/article/details/105597971