pint icon indicating copy to clipboard operation
pint copied to clipboard

Ugly ipython representation for Quantity

Open crusaderky opened this issue 4 years ago • 3 comments

The ipython output for Quantity looks like a mix of str() for the magnitude and repr() for the unit:

In[1]: q = pint.Quantity(1, "kg")
In[2]: str(q)
Out[2]: '1 kilogram'
In[3]: repr(q)
Out[3]: "<Quantity(1, 'kilogram')>"
In[4]: q
Out[4]: 1 <Unit('kilogram')>

crusaderky avatar Mar 12 '20 18:03 crusaderky

Can you also comment on what would have bean an esthetically pleasing ipython representation for Quantity?

5igno avatar May 21 '20 19:05 5igno

pick either repr or str, not a hybrid

crusaderky avatar May 22 '20 15:05 crusaderky

ipython seems to call FormattingQuantity._repr_pretty_ which delegates to both the magnitude and the unit, and since neither the magnitude nor the unit implement _repr_pretty_, the result is roughly

f"{repr(self.magnitude)} {repr(self.units)}"

To fix this, we probably need to figure out if we actually need the pretty version, and if yes, what it should look like.

keewis avatar Sep 18 '22 11:09 keewis