Python Memo

Class

@property

  • 自定义某个属性的一种简单方法是将它定义为一个property。 例如,下面的代码定义了一个property,增加对一个属性简单的类型检查
    class Person:
      def __init__(self, first_name):
          self._first_name = first_name
    
      # Getter function
      @property
      def first_name(self):
          return self._first_name
    
      # Setter function
      @first_name.setter
      def first_name(self, value):
          if not isinstance(value, str):
              raise TypeError('Expected a string')
          self._first_name = value
    
      # Deleter function (optional)
      @first_name.deleter
      def first_name(self):
          raise AttributeError("Can't delete attribute")
    
  • 你想将一个只读属性定义成一个property,并且只在访问的时候才会计算结果。 但是一旦被访问后,你希望结果值被缓存起来,不用每次都去计算。
class lazyproperty:
    def __init__(self, func):
        self.func = func

    def __get__(self, instance, cls):
        if instance is None:
            return self
        else:
            value = self.func(instance)
            setattr(instance, self.func.__name__, value)
            return value
import math

class Circle:
    def __init__(self, radius):
        self.radius = radius

    @lazyproperty
    def area(self):
        print('Computing area')
        return math.pi * self.radius ** 2

    @lazyproperty
    def perimeter(self):
        print('Computing perimeter')
        return 2 * math.pi * self.radius

不允许修改 property

def lazyproperty(func):
    name = '_lazy_' + func.__name__
    @property
    def lazy(self):
        if hasattr(self, name):
            return getattr(self, name)
        else:
            value = func(self)
            setattr(self, name, value)
            return value
    return lazy

简化数据结构的初始化

  • 如果还想支持关键字参数,可以将关键字参数设置为实例属性:
  • 你还能将不在 _fields 中的名称加入到属性中去
class Structure3:
    # Class variable that specifies expected fields
    _fields = []

    def __init__(self, *args, **kwargs):
        if len(args) != len(self._fields):
            raise TypeError('Expected {} arguments'.format(len(self._fields)))

        # Set the arguments
        for name, value in zip(self._fields, args):
            setattr(self, name, value)

        # Set the additional arguments (if any)
        extra_args = kwargs.keys() - self._fields
        for name in extra_args:
            setattr(self, name, kwargs.pop(name))

        if kwargs:
            raise TypeError('Duplicate values for {}'.format(','.join(kwargs)))

# Example use
if __name__ == '__main__':
    class Stock(Structure3):
        _fields = ['name', 'shares', 'price']

    s1 = Stock('ACME', 50, 91.1)
    s2 = Stock('ACME', 50, 91.1, date='8/2/2012')

__str__()__repr__()

  • 要改变一个实例的字符串表示,可重新定义它的 __str__()__repr__()方法。 如果 str() 没有被定义,那么就会使用 repr() 来代替输出。
class Pair:
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def __repr__(self):
        return 'Pair({0.x!r}, {0.y!r})'.format(self)

    def __str__(self):
        return '({0.x!s}, {0.y!s})'.format(self)
>>> p = Pair(3, 4)
>>> p
Pair(3, 4) # __repr__() output
>>> print(p)
(3, 4) # __str__() output
>>>

上面的 format() 方法的使用看上去很有趣,格式化代码 {0.x} 对应的是第1个参数的x属性。 因此,在下面的函数中,0实际上指的就是 self 本身:

def __repr__(self):
    return 'Pair({0.x!r}, {0.y!r})'.format(self)

References

  • https://python3-cookbook.readthedocs.io/