pint
pint copied to clipboard
Ugly ipython representation for Quantity
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')>
Can you also comment on what would have bean an esthetically pleasing ipython representation for Quantity?
pick either repr or str, not a hybrid
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.
I now get
q
1 kilogram
and I get <Quantity(1, 'kilogram')>
. Either way this looks fine.