Custom property, and supplementary delay function

@property  #area=property(area)

Descriptor is used to describe someone else, someone else's property agency, others are a class, the class will give rise to two products (instance, owner), both can call the get method descriptor, instance returns themselves, owner return None

Achieve delay function: delay calculation, also known as lazy evaluation

self.func .__ name__ call the function name

setattr(instance , self.func.__name__ , res)

@property

def area (): This corresponds to the two-step area = property (area)

m = property (m_get, m_set, m_delete) property is described, a parameter to be in line with this order (may be associated with mapping)

self.m equivalent self.m_get so

@AAA.setter  @AAA.delete

def AAA (): def AAA (): If only static properties, these two attributes decorators can add and delete properties

class Foo:
    def __init__(self,func):
        self.func=func
    def __set__(self, instance, value):
        print(123)
    def __delete__(self, instance):
        print(456)
    def __get__(self, instance, owner):
        print(789)
        return self.func(instance)
class Too:
    @Foo
    def area(self):
        return 111
t1=Too()
print(t1.area)
Descriptor custom property

Guess you like

Origin www.cnblogs.com/jintian/p/10958793.html
Recommended