1

I have an abstract class (called AbstractSparseVector) whose most important feature is a dictionary (called dict). So in the __repr__ function I just call str(self.dict).

Now the implementing subclasses all have different types of values in that dictionary. One just has a float and the other a tuple containing 3 elements: boolean, string, string. The strings of the latter class can contain special characters such as 'ö' but printing my AbstractSparseVector changes that character into '\xc3\xb6g'.

Now from this answer I understand that this happens because the dictionary calls the Tuple's __repr__ which returns the wrong '\xc3\xb6g' characters. If it called __str__ it would print correctly.

Is there any pretty way to achieve this?

So far my only solution is to iterate over the key, value pairs of the dictionary then over the tuple so that I can get the __str__ representation of these strings rather than the __repr__ presentation and piecing my own output string togeter.

That is ugly and also impractical because every subclass of AbstractSparseVector will have to implement its own __repr__ class.

Community
  • 1
  • 1
carence
  • 87
  • 9
  • 4
    `__repr__` is meant to produce *debug output*, which is usually then printed or written to a file. As such debug output for strings with non-ASCII data use *escape sequences* to ensure the data remains printable using ASCII characters only. What you are seeing is that process, and is usually desirable. – Martijn Pieters May 11 '15 at 10:28
  • What is your end-goal here; what is the information returned from `__repr__` being used for? If you are using it to show information to end-users, `__repr__` doesn't really fit that use case. – Martijn Pieters May 11 '15 at 10:30
  • I understand that, I was just hoping there would be a prettier way to get what I need in my case without iterating over each element to get the `__str__` representation. – carence May 11 '15 at 10:31
  • I actually use it to debug, I want to compare the resulting string to another, gold string that I know is correct, to make sure that my programme is running correctly. But the gold string uses the 'ö' correctly and it makes it thus hard to compare – carence May 11 '15 at 10:33
  • So why do you need a prettier version? Dictionaries have no `__str__` method, so `str(dictionary)` uses `dictionary.__repr__`. That's because dictionaries are not end-user representable data structures, you need to build your own for your needs (there is no one-size-fits-all user representation of dictionaries). As such the contents are also shown with `repr()`, not `str()` because, again, we are talking debug info here. – Martijn Pieters May 11 '15 at 10:33
  • You cannot do that with dictionaries anyway because their order is arbitrary. You cannot reliably build a 'gold string' in that case. But using '\xhh` escape sequences in your gold string should be *trivial* there. – Martijn Pieters May 11 '15 at 10:34
  • True, I did have to sort the dictionary afterwards separately, pretty messy. I assume the best option is just to write my own method. Thanks for clearly up some stuff that I seem to have misunderstood about `__repr__` – carence May 11 '15 at 10:40

0 Answers0