Built-attr attribute class attributes attr built class python

Built python class attributes attr

 
Foo class: 
    X =. 1 
    DEF the __init __ (Self, Y): 
        self.y Y = 

    DEF __getattr __ (Self, Item): 
        Print ( '----> from getattr: you find attribute does not exist') 


    DEF __setattr __ (Self , Key, value): 
        Print ( '----> from setattr') 
        # # This self.key value = infinite recursion, you think about 
        # self .__ dict __ [key] = value # should use it 

    def __delattr__ (Self, Item): 
        Print ( '----> from delattr') 
        # # infinite recursion del self.item 
        Self .__ dict __ POP (Item). 

#__setattr__ add / modify attributes will trigger its execution 
f1 = Foo (10) 
Print (f1 .__ dict__) # because you rewrite __setattr__, all assignment will trigger its run, you got nothing to write, that is simply not the assignment, unless you directly manipulate the properties dictionary, otherwise never be able to assign 
f1 =. 3 .Z 
Print (F1 .__ dict__ magic)

#__delattr__ delete a property when it will trigger 
f1 .__ dict __ [ 'a' ] = 3 # we can directly modify the attributes dictionary to complete the add / modify the properties of the operating 
del f1.a 
Print (f1 .__ dict__) 

#__getattr__ only the point of use and call the property when the property does not exist will trigger 
f1.xxxxxx
Foo class: 
    X =. 1 
    DEF the __init __ (Self, Y): 
        self.y Y = 

    DEF __getattr __ (Self, Item): 
        Print ( '----> from getattr: you find attribute does not exist') 


    DEF __setattr __ (Self , Key, value): 
        Print ( '----> from setattr') 
        # # This self.key value = infinite recursion, you think about 
        # self .__ dict __ [key] = value # should use it 

    def __delattr__ (Self, Item): 
        Print ( '----> from delattr') 
        # # infinite recursion del self.item 
        Self .__ dict __ POP (Item). 

#__setattr__ add / modify attributes will trigger its execution 
f1 = Foo (10) 
Print (f1 .__ dict__) # because you rewrite __setattr__, all assignment will trigger its run, you got nothing to write, that is simply not the assignment, unless you directly manipulate the properties dictionary, otherwise never be able to assign 
f1 =. 3 .Z 
Print (F1 .__ dict__ magic)

#__delattr__ delete a property when it will trigger 
f1 .__ dict __ [ 'a' ] = 3 # we can directly modify the attributes dictionary to complete the add / modify the properties of the operating 
del f1.a 
Print (f1 .__ dict__) 

#__getattr__ only the point of use and call the property when the property does not exist will trigger 
f1.xxxxxx

Guess you like

Origin www.cnblogs.com/NumerOne/p/11465187.html