首页 > 编程 > Python > 正文

python:hasattr(object, name)

2019-11-09 20:46:54
字体:
来源:转载
供稿:网友

这个函数是用来判断对象object的属性(name表示)是否存在。如果属性(name表示)存在,则返回True,否则返回False。参数object是一个对象,参数name是一个属性的字符串表示。

例子:

hasattr()

class Foo: def __init__(self): self.x = 123 def test(x): self.x = xfoo = Foo()PRint(hasattr(foo, 'x'))print(hasattr(foo, 'y'))print(hasattr(foo, 'test'))

输出结果如下:

TrueFalseTrue
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表