PythonTip >> 博文 >> chinaunix

python学习笔记 - @property

zihua 2014-01-17 18:01:03 点击: 790 | 收藏



点击(此处)折叠或打开

  1. class parrot(object):
  2.     def __init__(self):
  3.         self._voltage = 10

  4.     @property
  5.     def voltage(self):
  6.         return self._voltage

  7.     @voltage.setter
  8.     def voltage(self, new_value):
  9.         self._voltage = new_value;

  10. if __name__ == "__main__":
  11.     p = parrot()
  12.     print p.voltage
  13.     p.voltage = 12
  14.     print p.voltage

原文链接:http://blog.chinaunix.net/uid-9162199-id-4082437.html

作者:zihua | 分类: chinaunix | 标签: python | 阅读: 790 | 发布于: 2014-01-17 18时 |