How to identify numpy types in python?

前端 未结 6 769
生来不讨喜
生来不讨喜 2020-12-23 09:02

How can one reliably determine if an object has a numpy type?

I realize that this question goes against the philosophy of duck typing, but idea is to make sure a fun

6条回答
  •  眼角桃花
    2020-12-23 09:16

    Use the builtin type function to get the type, then you can use the __module__ property to find out where it was defined:

    >>> import numpy as np
    a = np.array([1, 2, 3])
    >>> type(a)
    
    >>> type(a).__module__
    'numpy'
    >>> type(a).__module__ == np.__name__
    True
    

提交回复
热议问题