属性

只读属性

只读属性,顾名思义,指的是只可读不可写的属性,之前我们定义的属性都是可读可写的,对于只读属性,我们需要使用 @property 修饰符来得到:

In [1]:

  1. class Leaf(object):
  2. def __init__(self, mass_mg):
  3. self.mass_mg = mass_mg
  4.  
  5. # 这样 mass_oz 就变成属性了
  6. @property
  7. def mass_oz(self):
  8. return self.mass_mg * 3.53e-5

这里 mass_oz 就是一个只读不写的属性(注意是属性不是方法),而 mass_mg 是可读写的属性:

In [2]:

  1. leaf = Leaf(200)
  2.  
  3. print leaf.mass_oz
  1. 0.00706

可以修改 mass_mg 属性来改变 mass_oz

In [3]:

  1. leaf.mass_mg = 150
  2.  
  3. print leaf.mass_oz
  1. 0.005295

是属性不是方法:

In [4]:

  1. leaf.mass_oz()
  1. ---------------------------------------------------------------------------
  2. TypeError Traceback (most recent call last)
  3. <ipython-input-4-aac6717ebc82> in <module>()
  4. ----> 1 leaf.mass_oz()
  5.  
  6. TypeError: 'float' object is not callable

而且是只读属性,不可写:

In [5]:

  1. leaf.mass_oz = 0.001
  1. ---------------------------------------------------------------------------
  2. AttributeError Traceback (most recent call last)
  3. <ipython-input-5-d232052cd2dc> in <module>()
  4. ----> 1 leaf.mass_oz = 0.001
  5.  
  6. AttributeError: can't set attribute

回到 forest 的例子,我们希望加入几个只读属性:

In [6]:

  1. import numpy as np
  2.  
  3. class Forest(object):
  4. """ Forest can grow trees which eventually die."""
  5. def __init__(self, size=(150,150)):
  6. self.size = size
  7. self.trees = np.zeros(self.size, dtype=bool)
  8. self.fires = np.zeros((self.size), dtype=bool)
  9.  
  10. def __repr__(self):
  11. my_repr = "{}(size={})".format(self.__class__.__name__, self.size)
  12. return my_repr
  13.  
  14. def __str__(self):
  15. return self.__class__.__name__
  16.  
  17. @property
  18. def num_cells(self):
  19. """Number of cells available for growing trees"""
  20. return np.prod(self.size)
  21.  
  22. @property
  23. def tree_fraction(self):
  24. """
  25. Fraction of trees
  26. """
  27. num_trees = self.trees.sum()
  28. return float(num_trees) / self.num_cells
  29.  
  30. @property
  31. def fire_fraction(self):
  32. """
  33. Fraction of fires
  34. """
  35. num_fires = self.fires.sum()
  36. return float(num_fires) / self.num_cells

查看属性:

In [7]:

  1. forest = Forest()
  2.  
  3. forest.num_cells

Out[7]:

  1. 22500

生成一个较小的森林:

In [8]:

  1. small_forest = Forest((10, 10))
  2. small_forest.num_cells

Out[8]:

  1. 100

初始状态下,树和火灾的比例都是 0:

In [9]:

  1. small_forest.tree_fraction

Out[9]:

  1. 0.0

In [10]:

  1. small_forest.fire_fraction

Out[10]:

  1. 0.0

可读写的属性

对于 @property 生成的只读属性,我们可以使用相应的 @attr.setter 修饰符来使得这个属性变成可写的:

In [11]:

  1. class Leaf(object):
  2. def __init__(self, mass_mg):
  3. self.mass_mg = mass_mg
  4.  
  5. # 这样 mass_oz 就变成属性了
  6. @property
  7. def mass_oz(self):
  8. return self.mass_mg * 3.53e-5
  9.  
  10. # 使用 mass_oz.setter 修饰符
  11. @mass_oz.setter
  12. def mass_oz(self, m_oz):
  13. self.mass_mg = m_oz / 3.53e-5

测试:

In [12]:

  1. leaf = Leaf(200)
  2. print leaf.mass_oz
  3.  
  4. leaf.mass_mg = 150
  5. print leaf.mass_oz
  1. 0.00706
  2. 0.005295

修改 mass_oz 属性:

In [13]:

  1. leaf.mass_oz = 0.01
  2. print leaf.mass_mg
  1. 283.28611898

一个等价的替代如下:

  1. class Leaf(object):
  2. def __init__(self, mass_mg):
  3. self.mass_mg = mass_mg
  4.  
  5. def get_mass_oz(self):
  6. return self.mass_mg * 3.53e-5
  7.  
  8. def set_mass_oz(self, m_oz):
  9. self.mass_mg = m_oz / 3.53e-5
  10.  
  11. mass_oz = property(get_mass_oz, set_mass_oz)

原文: https://nbviewer.jupyter.org/github/lijin-THU/notes-python/blob/master/08-object-oriented-programming/08.06-properties.ipynb