September 8, 2019 descriptors, agents

class Foo:
     DEF  __set__ (Self, instance, value): # Because Foo agent x, it triggers a set method 
        Print ( ' set methods ' , instance, value) # instance transmission is B1 
        instance. the __dict__ [ ' X ' ] = value # underlying implementation assignment, if there is no sentence, then b1 .__ dict__ print out is empty. 

class Bar: 
    X = Foo () # Foo () agent of X 
    DEF  the __init__ (Self, n-): 
        self.x = n- 

B1 = Bar (10 ) 
b1.x = 22 is
 Print (b1.x)
print(b1.__dict__)

》》》

set方法 <__main__.Bar object at 0x02D90A30> 10
set方法 <__main__.Bar object at 0x02D90A30> 22
22
{'x': 22}

Guess you like

Origin www.cnblogs.com/python1988/p/11488464.html