Descriptor underlying method can be realized most pyhon class characteristics, comprising: classmethod, staticmethd, property, __slots__
Descriptor is modified to define a class attribute class
class Lazyproperty: DEF the __init__ (Self, FUNC): Print ( ' >>>>> ' , FUNC) self.func = FUNC DEF __get__ (Self, instance, owner): # Self is Lazyproperty () generated object Print ( ' get method ' ) return self.func (instance) # instance meaning examples of delivery itself, owner class instance is generated class Room: # descriptor is modified to define the class # Area = Lazyproperty (Area) # descriptor operation, but the following @lazyproperty is doing the same thing DEF __init__(Self, name, width, length): self.name = name self.width = width self.length = length # the @Property # @ static properties have actually run in the area = property (area) equivalent to property do instantiation . May be a function, or may be based, is able to achieve decorative effects, achieve increased to the class descriptor Property = Area (Area) @Lazyproperty DEF Area (Self): return self.width * self.length R1 = Room ( ' CS ' , 2,4 ) Print (r1.area) # where the call is Lazyproperty (area) returns the assigned area, the non-trigger data descriptor # Print (r1.area.func (R1)) # purely manual trigger run
》》》》》》》》》》
get method
8