Access variables from main program in python

Question:

I’m new to this website, may I have some help on the following?

I have a main.py program that contains the dictionary loaddict.

I have a module outside of the main program that contains multiple functions which all of them requires the dictionary loaddict from the main program.

Is there a way to access the dictionary loaddict from multiple functions in this module without setting loaddict as a parameter for all of them?

The following code doesn’t work, as the remaining function still does not have access to loaddict from the function dgm even with the use of keyword global.

## main program (main.py)
## user inputs data into dictionary: loaddict = {some data}

import BeamDiagram.dgm(loaddict, other parameters)
   
## module (BeamDiagram.py)

def dgm(loaddict, other parameters): 
    global loaddict 
    ## some calculations, this part is fine

def function1(some parameters): 
    ## calculations that requires loaddict

def function2(some parameters): 
    ## calculations that requires loaddict

def function3(some parameters): 
    ## calculations that requires loaddict
Asked By: Hong Rui Ong

||

Answers:

Your mistake: the import in the script BeamDiagram.py

In my opinion your mistake is only the import instruction, so in your code it is necessary only a correct import that is the following:

from main import loaddict

Below I show you the 2 files that I have created in my system (both files are in the same folder /home/frank/stackoverflow).

main.py

loaddict = {'key1': 'value1'}

''' The function print the value associates to 'key1' in the dictionary loaddict 
'''
def print_dict():
    print(loaddict['key1'])

In main.py I have created the function print_dict() which is imported by the script BeamDiagram.py as it is imported the dictionary loaddict (see below for the code of BeamDiagram.py)

BeamDiagram.py

'''
Module BeamDiagram.py
'''

from main import loaddict, print_dict

''' In the function the parameter 'loaddict' has been removed...
'''
def dgm(other_parameters):
    # no global keyword inside the function
    print(loaddict['key1'])

''' function1 modify loaddict value and call a function from main.py
'''
def function1(some_parameters):
    # the following instruction is able to modify the value of associated to 'key1'
    loaddict['key1'] = 'value2'
    print_dict()    # print 'value2' on standard output

dgm('other_params')
function1('some_params')

The script BeamDiagram.py calls the functions dgm() and function1() and this means that:

  • it is possible the read access to loaddict (dgm())
  • it is possible the write access to loaddict (function1())
  • the modification of key1 value is visible in main.py, and in effect the function print_dict1() prints value2 which is the value of key1 after that function1() has made the write access to loaddict

Answered By: frankfalse
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.