Python用户定义的模块,从中导入系统模块

2024-04-19 05:14:50 发布

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

我已经创建了一个模块:test.py。里面看起来像:

import matplotlib.pyplot as plt   
N = 5
def test_img(data_loc):
    #F = load images from data_loc

    def plot_img(im):
        # a bunch of other codes
        plt.imshow(im)

    for i in range(N):
        plot_img(F(i))

(1)在某处使用它作为import testfrom test import test_img,我在哪里import matplotlib.pyplot as plt?它在test.py里面像现在一样吗

(2)在这种情况下,plot_img嵌套在test_img内,如果它只在test_img内使用,这有关系吗

(3)我在哪里声明常量N重要吗(这是一个例子,我有很多常量)?如果它在plot_img中使用,我在哪里声明它呢


Tags: frompytestimport声明imgdataplot
1条回答
网友
1楼 · 发布于 2024-04-19 05:14:50
  1. 是的,可以按原样导入matplotlib
  2. 它应该很好用
  3. 把常数放在任何地方。如果它们只在一个函数中使用,请考虑将它们放入其中。但你现在拥有的一切都是好的

相关问题 更多 >