0
votes

In my project I have 4 python files:

file1.py, file2.py file3.py, file4.py

test_list1 list I want to define globally this should accessible to all the files.

I mean,

if i will define list in file1.py

 test_list1 = []

and I am importing file1.py to file2.py, file3.py and file4.py

I need to append values to list test_list1 from file2.py , file3.py and file4.py

The execution of the script flow is file2.py followed by file3.py and followed by file4.py. and again file 2 is going to access

what is the best way to define variable this kind of different files.

I know my English language is worst . Please let me know if not understand the question properly.

1
import all files into a main file, and you can define test_list1 in the main file , then pass that variable to other functions from other files - Stack
... don't do this. Imported modules should not assume anything about the module calling them (that leads to headaches). Have the calling module pass it as a parameter instead. - Hugh Bothwell
@stack mail file is promising could you please give example or any link to refer and @ Hugh please provide example or docs - Bittu

1 Answers

0
votes

I think your file1.py is your main module and you define your test_list1 = [] there, If you use this list in functions, you can pass this through function parameter, like your_fuction(test_list1) in this case the best way is that you import only the function that you will use in your file1.py, like from file2 import your_function, your_function2, and when you were in file2.py and you use some function from file3.py do the same, from file3 import your_file3_function.