Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2441679
  • 博文数量: 328
  • 博客积分: 4302
  • 博客等级: 上校
  • 技术积分: 5486
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-01 11:14
个人简介

悲剧,绝对的悲剧,悲剧中的悲剧。

文章分类

全部博文(328)

文章存档

2017年(6)

2016年(18)

2015年(28)

2014年(73)

2013年(62)

2012年(58)

2011年(55)

2010年(28)

分类: Python/Ruby

2016-02-11 23:06:11

Property is a recommended way to set and get member variables, especially private ones. The reason is that all attributes are public in Python. Starting names with an underscore or two is just a warning that the given attribute is an implementation detail that may not stay the same in future versions of the code. It doesn't prevent you from actually getting or setting that attribute. Therefore, standard attribute access is the normal, Pythonic way of, well, accessing attributes.

The advantage of properties is that they are syntactically identical to attribute access, so you can change from one to another without any changes to client code. You could even have one version of a class that uses properties (say, for code-by-contract or debugging) and one that doesn't for production, without changing the code that uses it. At the same time, you don't have to write getters and setters for everything just in case you might need to better control access later.

Here are two different ways to use the property, which have the same effect. The first one is encouraged cause it is more clear, and don't forget to create the class as the descendent of "object"

  1. class Celsius(object):
  2.     def __init__(self, temperature = 0):
  3.         self._temperature = temperature

  4.     def to_fahrenheit(self):
  5.         return (self.temperature * 1.8) + 32

  6.     @property
  7.     def temperature(self):
  8.         print("Getting value")
  9.         return self._temperature

  10.     @temperature.setter
  11.     def temperature(self, value):
  12.         if value < -273:
  13.             raise ValueError("Temperature below -273 is not possible")
  14.         print("Setting value")
  15.         self._temperature = value


  16. print("this firts line\n")
  17. c = Celsius(5)
  18. c.temperature = 10
  19. deg = c.temperature
  20. print("temperature is %s\n" %deg)

Second one:

  1. class Celsius(object):
  2.     def __init__(self, temperature = 0):
  3.         print("init\n")
  4.         self.temperature = temperature

  5.     def to_fahrenheit(self):
  6.         return (self.temperature * 1.8) + 32

  7.     def get_temperature(self):
  8.         print("Getting value")
  9.         return self._temperature

  10.     def set_temperature(self, value):
  11.         if value < -273:
  12.             raise ValueError("Temperature below -273 is not possible")
  13.         print("Setting value")
  14.         self._temperature = value

  15.     temperature = property(get_temperature,set_temperature)

  16. print("this firts line\n")
  17. c = Celsius(5)
  18. deg = c.temperature
  19. print("temperature is %s\n" %deg)

  20. d = Celsius(10)
  21. deg = d.temperature
  22. print("temperature is %s\n" %deg)

阅读(1320) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~