python - 如何将 .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("*")
.
.
.
解决方案
也许像这样稍微重新排列您的代码:
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]
推荐阅读
- php - 为什么我的 if 语句不适用于所有 mysql 结果?
- javascript - GlideAjax 请求不返回任何响应
- css - 具有动态内容的 CSS 网格布局内的 Flexbox
- javascript - jQuery 多次自动单击按钮
- reactjs - 如何通过 ApolloClient 的变异提交 formik 表单?
- mysql - 优先选择数据库条目
- c# - 为什么 ETW 日志记录会在 2 秒后记录
- python - 我不能遍历嵌套字典
- laravel - laravel/lumen abort 继续中间件执行
- python - pythons关键字“in”是否检查“is”或“==”