Open pen, and write about, hasattr, getattr, setattr.

These three functions are to determine whether the object which has the property or the method (method may also be invoked property)

 

Had often written, written xx.getattr ..... remember is a function of the direct command head began to write.

In [23]: class Demo: 
    ...:     def __init__(self): 
    ...:         self.name = 'sidian' 
    ...:     def out_name(self): 
    ...:         return self.name 
    ...:                                                                                                         

In [24]:                                                                                                         

In [24]: dd = Demo()                                                                                             

In [25]: hasattr(dd,'name')                                                                                      
Out[25]: True

In [26]: hasattr(dd,'out_name')                                                                                  
Out[26]: True

In [27]: hasattr(dd,'age')                                                                                       
Out[27]: False

 Introduces hasatr, is to determine whether the object has the attribute, the value returned is Boll, True or False.

 

In [30]: getattr(dd, 'name')                                                                                     
Out[30]: 'sidian'

In [31]: getattr(dd, 'out_name')                                                                                 
Out[31]: <bound method Demo.out_name of <__main__.Demo object at 0x10a4a9290>>

In [32]: getattr(dd, 'out')                                                                                      
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-32-de58a5a0d8df> in <module>
----> 1 getattr(dd, 'out')

In [35]: getattr(dd, 'out','ok')                                                                                 
Out[35]: 'ok'

 The next test is getattr, although there get the name but temper is not good, but when he could not get inside the properties of an object, direct die for you, so you can add a third parameter, but did not get the first output three parameters.

In [37]: setattr(bb,'age',18)                                                                                    

In [38]: setattr(bb,'name','wangba')                                                                             

In [39]: getattr(bb,'age')                                                                                       
Out[39]: 18

In [40]: getattr(bb,'name')                                                                                      
Out[40]: 'wangba'

 setattr may be added to the object properties, which can also modify the properties of the specific operation is relatively simple.

Guess you like

Origin www.cnblogs.com/sidianok/p/11785031.html