Disable silent conversions in numpy

前端 未结 2 1637
梦如初夏
梦如初夏 2021-01-13 12:36

Is there a way to disable silent conversions in numpy?

import numpy as np
a = np.empty(10, int)
a[2] = 4     # OK
a[3] = 4.9   # Will silently convert to 4,          


        
2条回答
  •  孤街浪徒
    2021-01-13 13:13

    Just an idea.

    #Python 2.7.3
    >>> def test(value):
    ...     if '.' in str(value):
    ...         return str(value)
    ...     else:
    ...         return value
    ... 
    >>> a[3]=test(4.0)
    Traceback (most recent call last):
      File "", line 1, in 
    ValueError: invalid literal for long() with base 10: '4.0'
    

提交回复
热议问题