导入numpy和作为NP导入numpy之间的区别

导入numpy和作为NP导入numpy之间的区别

本文介绍了导入numpy和作为NP导入numpy之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解,应该尽可能使用

I understand that when possible one should use

import numpy as np

这有助于避免由于名称空间引起的任何冲突.但是我注意到,尽管下面的命令有效

This helps keep away any conflict due to namespaces. But I have noticed that while the command below works

import numpy.f2py as myf2py

以下内容不

import numpy as np
np.f2py #throws no module named f2py

有人可以解释一下吗?

推荐答案

numpy 是包的顶部名称,执行import numpy不会导入子模块numpy.f2py.

numpy is the top package name, and doing import numpy doesn't import submodule numpy.f2py.

当您执行import numpy时,它会创建一个指向numpy的链接,但是numpy没有进一步链接到f2py.当您执行import numpy.f2py

When you do import numpy it creats a link that points to numpy, but numpy is not further linked to f2py. The link is established when you do import numpy.f2py

在上面的代码中:

import numpy as np # np is an alias pointing to numpy, but at this point numpy is not linked to numpy.f2py
import numpy.f2py as myf2py # this command makes numpy link to numpy.f2py. myf2py is another alias pointing to numpy.f2py as well

这是import numpy.f2pyimport numpy.f2py as myf2py之间的区别:

  • import numpy.f2py
    • numpy 放入本地符号表(指向numpy),并将numpy链接到numpy.f2py
    • numpy numpy.f2py 均可访问
    • import numpy.f2py
      • put numpy into local symbol table(pointing to numpy), and numpy is linked to numpy.f2py
      • both numpy and numpy.f2py are accessible
      • my2py 放入本地符号表(指向numpy.f2py)
      • 其父numpy未添加到本地符号表中.因此,您不能直接访问numpy
      • put my2py into local symbol table(pointing to numpy.f2py)
      • Its parent numpy is not added into local symbol table. Therefore you can not access numpy directly

      这篇关于导入numpy和作为NP导入numpy之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 10:12