Python标准库:内置函数hasattr(object, name)

时间:2014-12-26 21:40:49   收藏:0   阅读:401

本函数是用来判断对象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  深圳


原文:http://blog.csdn.net/caimouse/article/details/42177341

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!