Different behavior of arithmetics on dtype float 'object' and 'float'

前端 未结 3 1165
一个人的身影
一个人的身影 2020-12-21 11:55

Hi guys im just a rookie in python (even in programming) so my question might sound very basic but i have a hard time to understand this.

Why is the selective behavi

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-21 12:30

    Not sure if this is the reason but my guess is:

    What if your objects were instances of your own homemade class? Numpy cannot do anything else but hope that you have defined a sqrt method in your class and let it do the work. So I assume that's what it does here, but in your case your objects are floats, and although np.sqrt(some_random_float) makes sense, some_random_float.sqrt() doesn't because the AttributeError: 'float' object has no attribute 'sqrt'.

    Typically numpy is designed and optimized for numerical computations, while using it with generic dtype=object can be sometime convenient, you shouldn't assume everything will always run seamlessly in this case...

提交回复
热议问题