简体   繁体   中英

How to pass data-structure residing in memory from module to module

I am writing a code, wherein I will store few files into the data-structure in memory, and will then use that data-structure in all the modules in order to access that data-structure. (time-saving)

Since the code is written in python, and is very big so its very hard to pass that data-structure from function-to-function.

Is there any possible way that I store the file-to-data-structure at the start of the code, and then I can use that data structure in any module, as that data structure is residing in the memory during the program execution.

Please help. Extremely important..!!

Let me give an example.

main.py..... This is the start of the project, here I store the complete file-into-datastructure. other_module.py..... This is some other module, where in i want to access that data structure which must be residing in the memory, as main.py is in execution and it had called other_module.py.

Notice, I cannot store file-to-data structure thing inside other_module because then it would be very time-consuming, as here i am giving a very small example, but I am actually dealing with a bigger problem.

Thanks.

and is very big so its very hard to pass that data-structure from function-to-function.

No, it isn't. Python variables don't work that way. You implicitly get a pointer.

Python variables and function arguments are always only references to objects, so don't worry about passing a large data structure as a function argument: the data is not copied, only a new reference is created.

However, if you know your code will always deal with only a single instance of the data structure, you can store it in a global variable. You could create a separate module that loads the data into a module-level variable, and import that module to main.py and other_module.py.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM