So I have the following code, I am trying to add some sort of metadata I suppose to an attribute of a class. My idea was that I would have a getter/setter in which the getter would return a float/int/string private variable but return some extra data with it, such as a verbose name.
This works perfectly until I use the setter, which once used seems to ignore the setter and getter.
Does anyone know why what I am doing is not working? Or if there is a better way to go about what I am trying to do?
Many thanks!
class Param(float):
def __new__(self,name,value):
return float.__new__(self,value)
def __init__(self,name,value):
self.__name = name
name = property(lambda self: self.__name)
class Beer:
def __init__(self, temp):
self.__T = temp
@property
def temp(self):
return Param('Beer Temperature', self.__T)
@temp.setter
def temp(self,value):
self.__T = float(value)
print b.temp.name, b.temp.__class__
print b.temp * 4.5
print b.temp.name, b.temp.__class__
b.temp = 101
print b.temp.name, b.temp.__class__
In python 2, properties work properly only with new-style classes (derived from
object):