首页 > 解决方案 > 如何将 .py 文件中的变量导入列表,以便可以使用该列表中的随机模块?

问题描述

我有一个 python 文件,其中包含大约 60 行变量,这些变量是文件导入(用于 pygame)。这是imports.py文件,它只包含变量,没有其他内容。如何将这些导入到我的列表中main.py

进口.py

import pygame

pygame.init()
one_surf = pygame.image.load("*")

two_surf = pygame.image.load("*")

three_surf = pygame.image.load("*")

four_surf = pygame.image.load("*")

five_surf = pygame.image.load("*")
.
.
.

标签: pythonpython-3.xlist

解决方案


也许像这样稍微重新排列您的代码:

imports.py

import pygame

pygame.init()

surf = [

  pygame.image.load("*"),

  pygame.image.load("*"),

  pygame.image.load("*"),

  pygame.image.load("*"),

  pygame.image.load("*"),

... ]

something_else.py

from imports import surf

然后您可以访问以前调用three_surf的内容surf[2]


推荐阅读