-
-
Notifications
You must be signed in to change notification settings - Fork 11.3k
Open
Labels
Description
In many wonderful cases an ndarray can be used in place of a Python float and Just Work.
But not in one case:
import numpy as np
n = 1.23
print('{0:.6} AU'.format(n))
n = np.array([1.23, 4.56])
print('{0:.6} AU'.format(n))
The output of the above code, at least under Python 3.4, is:
1.23 AU
Traceback (most recent call last):
File "tmp9.py", line 7, in <module>
print('{0:.6} AU'.format(n))
TypeError: non-empty format string passed to object.__format__
It would be a great convenience if the ndarray grew a __format__()
method that understood the tiny mini-language of float formatting, and used the number of digits of precision specified there to make its own call to the standard NumPy vector array formatting. Users could control array appearance on the screen using a Python standard that many programmers already understand.
endolith, moorepants, mattayes, duncanwp, NeilGirdhar and 9 more