What I would like
In this example, I would like to see the unicode string without using print
:
In [1]: a = u's·A/m'
In [2]: type(a)
Out[2]: str
In [3]: a
Out[3]: 's\xc2\xb7A/m'
In [4]: print a
s·A/m
How to force string __repr__
not to display s\xc2\xb7A/m
but s·A/m
instead?
What is the use-case?
I have a class that represents a number in association with its units for example:
class MyNumber(float):
def __new__(cls, ...):
...
def __repr__(self):
return str(self) + str(self.units)
When I am working in IPython I would like to quickly see the content of instance:
>>> a = MyNumber('23.43', ampere=1, second=1, meter=-1)
>>> a
23.43 s·A/m
Instead I get an exception:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 11: ordinal not in range(128)
And If I try to manually see the content of my __repr__
I get this:
>>>a.__repr__()
23.43 s\xc2\xb7A/m