[Chicago] Property Getter & Setter Problem
Oren Livne
livne at uchicago.edu
Fri Jun 1 18:33:43 CEST 2012
Dear All,
Sorry if this is too basic. I'm trying to understand how get & set
works. I created a class "Clazz" with a mutable property "x" and
immutable property "voltage". When I run the following module, I get
voltage <property object at 0x7f31cfea55d0> x <property object at
0x7f31cfea5628>
voltage <property object at 0x7f31cfea55d0> x 10
Traceback (most recent call last):
File "/home/oren/ober/impute/tests/SimpleClasses.py", line 39, in
<module>
assert c.voltage == 100000, 'Property not set properly'
AssertionError: Property not set properly
I am expecting to get voltage 100000 x 200 on the first line.
Thank you so much,
Oren
class Clazz(object):
'''Illustrates a property with a getter and setter.'''
def __init__(self):
print 'Creating instance of Clazz'
self._x = None
self.x = 200
self._voltage = 100000
@property
def voltage(self):
"""Get the current voltage."""
return self._voltage
@property
def x(self):
"""I'm the 'x' property."""
return self._x
@x.setter
def x(self, value):
self._x = value
@x.deleter
def x(self):
del self._x
if __name__ == "__main__":
c = Clazz;
print 'voltage', c.voltage, 'x', c.x
c.x = 10
print 'voltage', c.voltage, 'x', c.x
assert c.voltage == 100000, 'Property not set properly'
More information about the Chicago
mailing list