zoukankan      html  css  js  c++  java
  • 自定制property

    class Lazyproperty:
    def __init__(self, func):
    self.func = func

    def __get__(self, instance, owner):
    print('get')
    # print(instance)
    # print(owner)
    if instance is None:
    return self

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


    class Room:
    def __init__(self, width, length):
    self.width = width
    self.length = length

    @Lazyproperty
    def area(self):
    return self.width * self.length

    @property
    def area1(self):
    return self.width * self.length


    r = Room(1, 2)

    # print(Room.area)

    print(r.area1)

    print(r.area)
    print(r.__dict__)
    print(r.area)
  • 相关阅读:
    20210131
    20210130
    20210129
    20210128
    20210127
    例3-7
    例3-5
    例3-4
    例3-3
    例3-2
  • 原文地址:https://www.cnblogs.com/majianyu/p/10255821.html
Copyright © 2011-2022 走看看