Problems with properties

Michael Schneider michaelschneider at fuse.net
Fri Oct 14 18:43:10 CEST 2005


Thanks to all,  I added the object as a subclass (should this be 
required for 2.4.1 ???)

I also switched to the decorator with the @property syntax

Thank you very much for the help for adding @property to the language.

what a great language :-)

Mike


Michael Schneider wrote:
> Hello All,
> 
> I have been working on learning how to use python properties.
> 
> The get property access is working, but the the set
> property is not working.
> 
> Rather then dispatching the property assignment to setNothing, the
> property object is being replaced with a string.
> 
> I must be doing something very stupid here.
> 
> Could someone please point out my error, I have dents in my forehead
> for this one.
> 
> Thanks,
> Mike
> 
> 
> 
> ------------------------------------------------------------------
> 
> from unittest import TestCase
> import unittest
> 
> 
> class Task:
>   def __init__(self,command):
>     self._command = command
> 
>   def setNothing(self, value):
>     raise AttributeError
> 
>   def getCommand(self):
>     return self._command
> 
>   command=property(getCommand, setNothing)
> 
> 
> class taskTest(TestCase):
> 
>     def testTask(self):
>         t = Task("dir c:")
>         c = t.command
>         self.assertEquals("dir c:", c)
> 
>         # should fail, but doesn't
>         t.command = "foo Bar"
> 
>         self.assertEquals("dir c:", t.command)
> 
> 
> 
> if __name__ == "__main__":
>     unittest.main()


-- 
The greatest performance improvement occurs on the transition of from 
the non-working state to the working state.



More information about the Python-list mailing list