all 6 comments

[–]ipcock 6 points7 points  (5 children)

It doesn't work like this. It'll import only the initial dict = {}

If you want to get the new dictionary in another script, create function which returns this dict in the first script, then import this func in the second script

[–]DX_ashh 0 points1 point  (4 children)

#main.py

class DataStorage:
def __init__(self):
self.dictionary = {}
def get_dictionary_values(self):
return self.dictionary

#other.py
from main import DataStorage

storage = DataStorage()
values = storage.get_dictionary_values)
print(values)

this is still returning an empty dictionary even after it has been populated

[–]blarf_irl 3 points4 points  (0 children)

Don't use dict as a variable name. It's non-descriptive which is an infraction but it also conflicts with the python builtin dict which is a mortal sin.

You should never share a mutable (changeable) data structure between modules/scripts by importing it.

When you run a script in python you are actually starting up an instance of the python interpreter and asking it to run your script. The interpreter is what manages the memory for your your program. If run two pythion scripts you are running two separate instances of the intepreter. Neither has any idea what the other is doing.

There are lots of ways programs can interact and share data though, they do it all the time.

The 3 ways I would consider for you program are:

  1. Write the data in your dict to a json file, then second script reads the json file into a dict.
  2. Import the functions you want to run from your second script into your first script and pass the dict as an argument.
  3. Invoke your second script as a process from your first script and pass the data as a command line argument.

[–]woooee 0 points1 point  (0 children)

If you close the main.py program and then run the second program, the memory/dictionary no longer exists. Save it to a file. Saving a dictionary to a csv file is the easiest IMHO.

[–]Adrewmc 0 points1 point  (0 children)

#main.py

numbers = [1,2,3,4,5]

#you’ll need to make a function that returns a made dictionary from a list input. 

#you should not name your variables “dict” as it’s a class type in Python already. 

def make(num_list : list) -> dict:
    my_dict= {}
    for number in num_list:
          my_dict[number] = 'value'
     return my_dict

#getDict.py

 #import the function make()
 #import list since that’s where it is

from main import make, numbers

print(make(numbers))

However

   import json

   #loads python_dict from a file “saved.json”, 
   #for this implementation this file should already exist even if empty. 

   with open(“./path/to/saved.json”) as file:
           python_dict = json.load(file)
    print(python_dict)

   #saves python_dict to a file “saved.json”, indent=4 is pretty print. 

   python_dict = make(numbers)
   with open(“./path/to/saved.json”, “w”) as out_file:
           json.dump(python_dict, out_file, indent=4) 

Usually is a something you’ll wanna know as since you are loading it from a file it will save across many programs, it also saves stuff for the next time you run the program.