Python: confusion between types and dtypes

前端 未结 3 760
死守一世寂寞
死守一世寂寞 2021-01-11 21:03

Suppose I enter:

a = uint8(200)
a*2

Then the result is 400, and it is recast to be of type uint16.

However:

a = arr         


        
3条回答
  •  时光说笑
    2021-01-11 21:09

    A numpy array contains elements of the same type, so np.array([200],dtype=uint8) is an array with one value of type uint8. When you do np.uint8(200), you don't have an array, only a single value. This make a huge difference.

    When performing some operation on the array, the type stays the same, irrespective of a single value overflows or not. Automatic upcasting in arrays is forbidden, as the size of the whole array has to change. This is only done if the user explicitly wants that. When performing an operation on a single value, it can easily upcast, not influencing other values.

提交回复
热议问题