python - 如何创建可通过多个文件更新和访问的变量

标签 python file list global

我是Python新手,仍在学习中,对于这个菜鸟问题深表歉意。我目前正在开发一个跨多个文件的程序,其中一个是“主文件”,这是唯一运行的文件。我正在尝试创建一个名为 mainList 的变量,并使其在所有文件中均可访问。我希望这样,如果我在一个文件中更改它,它就会在其他文件中更新。 我尝试使用全局,但它不会更新所有文件中的列表。 这是我的三个文件的非常简化的版本:

这是主文件:#我只运行主文件

import file1
import file2
file2.search(mainList)

这是文件1:

import file2
class Library:
#Alot of code I will not post here, but it reads data from a file and then stores
#the instances in mainList by using a for loop and then appending to the list.
#This code reads data from a text file and stores the data in every line in a 
#different instance at an encapsulated field called data, the instances are stored 
#the mainList.

这是文件2:

import file1
global mainList
mainList = []
#Alot of code I will not post here for simplicity sake
def search(LIST):
    temp = input("enter what you want to search for")
    tempList = []
    for i in LIST:
        if str(i._file1__data) == temp:
            tempList.append(i)
    for i in tempList:
        print(tempList._file1__data)

最佳答案

只需导入变量或设置一个工厂函数即可将其输出。例如,如果您有一个类似的目录结构

project/
  __init__.py
  app.py
  config.py

app.py 可能需要从 config.py 导入 mainList。好吧,在 app.py

from config import mainList

瞧。您现在可以在 app.py 中访问它。您还可以导入整个文件并访问单个变量或函数,如下所示:

import config
mainList = config.mainList

如果它位于子目录中,例如

project/
  __init__.py
  app.py
  otherstuff/
    __init__.py
    config.py 

你可以

from otherstuff.config import mainList

但是您的子目录中必须有一个 __init__.py 才能执行此操作(即使 __init__.py 完全为空)。

更新

好吧,我认为您想要做的是将 mainList 保留在自己的文件中,并使用您的 search(list) 函数将其“存储”在那里。然后,file1file2 相互引用(当您这样做时,称为循环导入,这很少是您想要的)。

问题是,用这样的目录结构来实现你想要的

app/
  mainFile.py
  file1.py
  file2.py

您需要在 mainFile.py 中做的就是

# mainFile.py
import file1

file1.py

# file1.py
import file2
# Maybe assign some variables for easier access
mainList = file2.mainList
search = file2.search
# And your code
class Library:
    pass

file2.py 不应该依赖于 file1.py 或导入流中任何更高级别的其他文件中的任何信息,并且它应该设置为获取任何列表并搜索它,而不仅仅是file1.py中的一个。 When you import a second file that in turn imports the first file, it can cause you a lot of headaches in Python (正如我之前提到的“循环导入”;有关它们的信息可以在文章底部找到)。如果 file2.py 中的内容确实或必须依赖于 file1.py,那么您实际上应该将其内容移至 file1.py 中。

现在您可以访问 mainFile.py 中的所有数据和结构,您可以按照自己的意愿操作它们。当解释器将 mainFile.py 作为 '__main__' 运行时,它只关心 mainFile.py 中的内容。由于您导入了 file1.pyfile2.py,它们就在其中并且可以在您的表达式中使用。需要搜索列表吗?创建一个 Library() 实例,然后通过它进行 search() ,可能像这样 (mainFile.py):

# mainFile.py
import file1

library = file1.Library()
search = file1.search    # Remember we imported search() from file2.py into file1.py

# Write your logic here

同样,当解释器运行时,只有 mainFile.py 中的内容才重要,并且您在那里拥有所需的一切,所以一切都很好。 search() 和其他特定于模块的变量、函数、方法或属性不能按照您在原始问题中编写它们的方式相互依赖;如果他们这样做,事情就会开始变得非常困惑。

关于python - 如何创建可通过多个文件更新和访问的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21081806/

相关文章:

python - 如何使用 Matplotlib GUI 而不是命令行提示来提示用户输入

python - 如何为 Cloud ML Engine 打包词汇文件

c - 需要帮助打印按天排序的文件

html - 显示列表的问题 - 样式

python - python 列表中每个元素的唯一索引值

python - 如何循环Beautiful Soup元素来获取属性值

python - 使用 map(int, raw_input().split())

python - 在 ConfigParser Python 中使用冒号

java - 如何使用 ZipEntry 读取位于另一个 zip 文件中的一个 zip 文件中的数据?

list - 如何将元素移动到groovy中列表的第一个位置