SciPy/NumPy import guideline

前端 未结 3 503
猫巷女王i
猫巷女王i 2021-02-08 09:10

Notice: I checked for duplicate and nothing clearly answers my question. I trust you\'ll let me know if I missed something!

In an effort to clean up my code, I have been

3条回答
  •  鱼传尺愫
    2021-02-08 09:57

    I recommend doing something like

    import numpy as np
    import scipy as sp
    

    instead. It is always dangerous to do from ... import * especially with large modules such as numpy and scipy. The following illustrates why:

    >>> any(['foo'])
    True
    >>> from scipy import *
    >>> any(['foo'])
    
    Traceback (most recent call last):
      File "", line 1, in 
         any(['foo'])
      File "C:\Python27\lib\site-packages\numpy\core\fromnumeric.py", line 1575, in any
        return _wrapit(a, 'any', axis, out)
      File "C:\Python27\lib\site-packages\numpy\core\fromnumeric.py", line 37, in _wrapit
        result = getattr(asarray(obj),method)(*args, **kwds)
    TypeError: cannot perform reduce with flexible type
    

    What happens here? The standard python builtin function any is replaced by scipy.any which has different behavior. That might break any code that uses the standard any.

提交回复
热议问题