Issue
I am trying to fill the gap in my understanding of Pythons property()
.
Below is the code that I have come up with, to understand property()
:
class Temperature:
def __init__(self, temp = 10):
self.set_temp(temp)
def to_farenheit(self):
return (self._temp * 1.8) + 32
def get_temp(self):
print "getting temperature value"
return self._temp
def set_temp(self, temp):
print "setting temperature value"
if temp < -237:
raise ValueError("this shud be above -273")
else:
self._temp = temp
temps = property(get_temp, set_temp)
I execute the above class and do the following:
>>> t = Temperature()
setting temperature value
>>> t.temps
getting temperature value
10
>>> t.temps = 13
>>> t.temps
13
>>> t.get_temp()
getting temperature value
10
>>>
As you can see above, when I try to set temp
value by assigning t.temps = 13
the set_temp()
function is not getting called which I was expecting would be called because of property()
functionality. Also I am ending up with 2 different values for variable temp
What am I missing?
Solution
It's simply because you use Python 2 and forgot to subclass object
. In your case property
simply doesn't work because it's an old-style class.
Better you subclass object
:
class Temperature(object):
...
Or even better: Use Python 3. Python 3 doesn't have old-style classes anymore and you can omit the (object)
part because it's implicit.
However you really shouldn't define get_temp
or set_temp
functions when you could use the decorator syntax. And you definitely shouldn't call them directly.
This would be much more pythonic:
class Temperature(object):
def __init__(self, temp = 10):
self.temps = temp
def to_farenheit(self):
return (self._temp * 1.8) + 32
@property
def temps(self):
print("getting temperature value")
return self._temp
@temps.setter
def temps(self, temp):
print("setting temperature value")
if temp < -237:
raise ValueError("this shud be above -273")
else:
self._temp = temp
That example will work on Python 2 and Python 3.
Answered By - MSeifert
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.