加载具有依赖类的模块

2024-04-27 03:22:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我是Python新手,如果我在写这个问题时没有使用适当的白话,我很抱歉。我在Windows机器上使用python3.6.1。我提供了一个工作的例子,我的问题。你知道吗

假设我编写了一个保存在Demo文件中的模块_职能部门. 它包含以下功能:

def chebyshev_nodes(degree, domain):
    return Chebyshev.basis(degree,domain).roots()

然后运行以下脚本:

from numpy.polynomial.chebyshev import Chebyshev
from Demo_func import chebyshev_nodes

chebyshev_nodes(5, [1,5])

它会产生以下错误:

NameError: name 'Chebyshev' is not defined

如果我像下面那样在脚本中编写chebyshev\u nodes函数,那么它就可以正常工作了。你知道吗

from numpy.polynomial.chebyshev import Chebyshev
def chebyshev_nodes(degree, domain):
    return Chebyshev.basis(degree,domain).roots()

chebyshev_nodes(5, [1,5])

我的理解是,进口切比雪夫是全球性的。但不知怎么的,它确实在我的模块Demo\u func中运行。如何编写依赖于Chebyshev类的模块?你知道吗


Tags: 模块fromimportnumpy脚本returndemodomain
1条回答
网友
1楼 · 发布于 2024-04-27 03:22:04

正如user2357112在上面的注释中指出的,模块的名称是全局初始化的,但名称不是。解决方案包括在演示的函数中加载Chebyshez类_职能部门文件。你知道吗

def chebyshev_nodes(degree, domain):
    from numpy.polynomial.chebyshev import Chebyshev
    return Chebyshev.basis(degree,domain).roots()

相关问题 更多 >