[Chicago] Property Getter & Setter Problem

Jeffery Smith jeff at allthingsdork.com
Fri Jun 1 18:47:18 CEST 2012


You didn't instantiate the class into an object. Simple typo.

c = Clazz()



On Fri, Jun 1, 2012 at 11:33 AM, Oren Livne <livne at uchicago.edu> wrote:

> 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'
>
> ______________________________**_________________
> Chicago mailing list
> Chicago at python.org
> http://mail.python.org/**mailman/listinfo/chicago<http://mail.python.org/mailman/listinfo/chicago>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/chicago/attachments/20120601/3846983a/attachment.html>


More information about the Chicago mailing list