Are Python properties broken?

Posted by jacob on Stack Overflow See other posts from Stack Overflow or by jacob
Published on 2010-03-17T03:23:12Z Indexed on 2010/03/17 3:31 UTC
Read the original article Hit count: 264

Filed under:
|

How can it be that this test case

import unittest

class PropTest(unittest.TestCase):
    def test(self):
        class C():
            val = 'initial val'

            def get_p(self):
                return self.val

            def set_p(self, prop):
                if prop == 'legal val':
                    self.val = prop

            prop=property(fget=get_p, fset=set_p)

        c=C()
        self.assertEqual('initial val', c.prop)

        c.prop='legal val'
        self.assertEqual('legal val', c.prop)

        c.prop='illegal val'
        self.assertNotEqual('illegal val', c.prop)

fails as below?

Failure
Traceback (most recent call last):
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/unittest.py", line 279, in run
    testMethod()
  File "/Users/jacob/aau/admissions_proj/admissions/plain_old_unit_tests.py", line 24, in test
    self.assertNotEqual('illegal val', c.prop)
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/unittest.py", line 358, in failIfEqual
    (msg or '%r == %r' % (first, second))
AssertionError: 'illegal val' == 'illegal val'

© Stack Overflow or respective owner

Related posts about python

Related posts about property