[NumPy-Tickets] [NumPy] #1675: No support for `__format__` in scalars/arrays

NumPy Trac numpy-tickets@scipy....
Mon Nov 15 13:34:43 CST 2010


#1675: No support for `__format__` in scalars/arrays
------------------------+---------------------------------------------------
 Reporter:  pv          |       Owner:  somebody
     Type:  defect      |      Status:  new     
 Priority:  normal      |   Milestone:  2.0.0   
Component:  numpy.core  |     Version:  1.5.0   
 Keywords:              |  
------------------------+---------------------------------------------------
 Numpy scalars and arrays don't work properly in the `format()` string
 formatting context:
 {{{
 >>> '{0:d}'.format(np.int16(1))
 Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
 ValueError: Unknown format code 'd' for object of type 'str'
 }}}
 The reason seems to be that the `__format__` method is inherited from
 `object`, and it converts the item to string via `str` first.

 The correct fix seems to be to implement a properly formatting-code aware
 `__format__` ourselves. For most types, this can probably be done directly
 by calling PyInt's or PyFloat's routine. However, for e.g. long doubles we
 might want to implement the formatting ourselves.

-- 
Ticket URL: <http://projects.scipy.org/numpy/ticket/1675>
NumPy <http://projects.scipy.org/numpy>
My example project


More information about the NumPy-Tickets mailing list