Problems with properties

Peter Otten __peter__ at web.de
Fri Oct 14 18:04:01 CEST 2005


Michael Schneider wrote:

> Rather then dispatching the property assignment to setNothing, the
> property object is being replaced with a string.

properties are for newstyle classes only (i. e. classes that inherit from
object).

from unittest import TestCase
import unittest

class Task(object):
    def __init__(self,command):
        self._command = command

    def setNothing(self, value):
        raise AttributeError

    def getCommand(self):
        return self._command

    command=property(getCommand, setNothing)
    # or just
    # command=property(getCommand)


class taskTest(TestCase):

     def testTask(self):
         t = Task("dir c:")
         c = t.command
         self.assertEquals("dir c:", c)

         def set():
             t.command = "foo Bar"
         self.assertRaises(AttributeError, set)

         self.assertEquals("dir c:", t.command)



if __name__ == "__main__":
     unittest.main()

Peter



More information about the Python-list mailing list