[Numpy-discussion] Implementing __format__
Pauli Virtanen
pav at iki.fi
Mon Nov 15 14:35:54 EST 2010
On Mon, 15 Nov 2010 09:20:43 -0600, Robert Kern wrote:
[clip]
> Correct. On a 64-bit system, numpy.int32 does not subtype from int. The
> format codes do strict type-checking.
One can argue that this is a bug in Python or Numpy:
"%d" % numpy.int16(1)
"{0:d}".format(numpy.int16(1))
the first one works fine whereas the second does not, and it is not clear
what is gained by disallowing the latter.
[To answer the OP -- the workaround is to use the "%d" formatting codes,
not the format() method, which is a recent addition in Python.]
To make it work via changes in Numpy: scalars should implement a
__format__ method. Two choices: either we parse the formatting string
ourselves, or forward formatting to Python. The PITA in implementing this
is in parsing the format string.
Doing formatting ourselves would allow e.g. formatting of long doubles
properly, which cannot be done via the %-syntax.
http://projects.scipy.org/numpy/ticket/1675
--
Pauli Virtanen
More information about the NumPy-Discussion
mailing list