Python的hasattr() getattr() setattr() 函数
2017-05-27 11:37
357 查看
class C(object): def __init__(self): self.a = 'hello' self.b = 'world' self.foo = 100 def get(self): return self.a if __name__ == '__main__': c = C() #判断一个对象里面是否有name属性或者name方法,返回BOOL值 print(hasattr(c, 'bar')) #获取对象object的属性或者方法,如果存在打印出来,如果不存在,打印出默认值,默认值可选。 print(getattr(c,'fcc','hahah')) #给对象的属性赋值,若属性不存在,先创建再赋值。 setattr(c,'name','toby') print(dir(c)) print(c.name)
相关文章推荐
- Python标准库:内置函数hasattr() getattr() setattr() 函数使用方法详解
- Python的hasattr() getattr() setattr() 函数使用方法详解
- Python的hasattr() getattr() setattr() 函数使用方法详解
- Python的hasattr() getattr() setattr() 函数使用方法详解
- Python的hasattr() getattr() setattr() 函数使用方法详解
- Python的hasattr() getattr() setattr() 函数使用方法详解
- Python的hasattr() getattr() setattr() 函数使用方法详解
- Python的hasattr() getattr() setattr() 函数使用方法详解
- Python的hasattr() getattr() setattr() 函数使用方法详解
- Python的hasattr() getattr() setattr() 函数使用方法详解
- Python的hasattr() getattr() setattr() 函数使用方法详解
- python hasattr() getattr() setattr()函数的使用
- Python的hasattr() getattr() setattr() 函数使用方法详解
- Python的hasattr() getattr() setattr() 函数使用方法详解
- python中hasattr()、getattr()、setattr()函数的使用
- Python的hasattr() getattr() setattr() 函数使用方法(简介)
- Python的hasattr() getattr() setattr() 函数使用方法详解
- python 面向对象中一些常用内建函数【getattr(), setattr(). hasattr(). delattr()...】
- Python中hasattr() getattr() setattr() 函数使用方法详解
- python: hasattr()、setattr()、getattr()、delattr() 内建函数