Dict __repr __ () in python2 and python3

I am transferring the python library from python 2 only in python 2 and 3 to one code base (2.6, 2.7 and 3.3+). The main problem is that many tests use something like this:

def test(self):
 example = {u'foo': u'bar'}
 self.assertEqual(str(example), "{u'foo': u'bar'}")

which works in python 2 but throws an exception in python3:

 AssertionError: "{'foo': 'bar'}" != "{u'foo': u'bar'}"

Is there a standard way to solve these problems besides the “different test”? overload __repr__?

+4
source share
2 answers

Get rid of these tests; they are next to useless:

  • Python dict.__repr__. Python ; . Python , , .

  • Python ; , .

    , Python 3.3 -, , . . PYTHONHASHSEED.

API , , self.assertEqual(); assertDictEqual(), , .

Python 3.3 u'foo' str, {u'foo': u'bar} Python 2.6, 2.7 3.3 .

+8

, , - : , .

- , :

self.assertEqual(example, {u'foo': u'bar'})

2.x, 3.x( 3.3+, , u , ).

+3

All Articles