Python标准库:内置函数hasattr(object, name)
Python标准库:内置函数hasattr(object, name)
本函数是用来判断对象object的属性(name表示)是否存在。如果属性(name表示)存在,则返回True,否则返回False。参数object是一个对象,参数name是一个属性的字符串表示。
例子:
#hasattr() class Foo: def __init__(self): self.x = 123 def test(x): self.x = x foo = Foo() print(hasattr(foo, 'x')) print(hasattr(foo, 'y')) print(hasattr(foo, 'test'))
输出结果如下:
True
False
True
蔡军生 QQ:9073204 深圳
相关内容
- Python标准库:内置函数format(value[, format_spec])
- Python标准库:内置函数format(value[, format_spec])
- python lambda 函数 学习
- python获得线程调用函数的返回值
- python获得线程调用函数的返回值
- Python标准库:内置函数exec(object[, globals[, locals]])
- Python标准库:内置函数eval(expression, globals=None, locals=N
- Python标准库:内置函数dict(iterable, **kwarg)
- Python标准库:内置函数classmethod(function)
- Python标准库:内置函数compile(source, filename, mode, flags=0
评论关闭