0

I have the following global dictionary

global configurationFileInfo_saved
configurationFileInfo_saved = {
        'True_key': 'True',
        'False_key': 'False',
        'filename': "Configuration-noMeta" + extnt,
        'os_key': "os",
        'os': "windows",
        'os_windowsCode': "windows",
        'os_linuxCode': "linux",
        'guiEnabled': 'True',
        'guiEn_key': "GUI",
        'allowCustom': 'True',
        'allowCustom_key': "allowCustomQuizConfiguration",
        'allOrPart': "a",
        'allOrPart_key': "questions_partOrAll",
        'allOrPart_allCode': "a",
        'allOrPart_partCode': "p",
        'questionAmountDivisionFactor': 2,
        'questionAmountDivisionFactor_key': "divisionFactor",
        'mode': "e",
        'mode_key': "mode",
        'mode_noDeduction_code': "noDeductions",
        'mode_allowDeductions_code': "allowDeductions",
        'deductionsPerIncorrect': 1,
        'deductionsPerIncorrect_key': "allowDeductions_pointDeduction_perIncorrectResponse",
        'loc': "C:\\Program Files (x86)\\Quizzing Application <Version>\\Admin\\Application Files\\dist\\Main\\",
        'loc_key': "location",
        'title': "Quizzing Appliaction <Version> -- By Geetansh Gautam",
        'title_key': "title"

This is where the dictionary is being accessed:

config_onBoot_keys = list(configSaved(True, False, None).keys()) config_onBoot_vals = list(configSaved(True, False, None).values())

configSaved(False, True, configurationFileInfo)

configSaved (Tempoarary function for reading andd writing):

def configSaved(get, save, saveDict):
    if get:
        return configurationFileInfo_saved
    elif save:
        configurationFileInfo_saved = saveDict

When I access the dictionary is a function later I get the following error:

UnboundLocalError: local variable 'configurationFileInfo_saved' referenced before assignment

What am I doing wrong?

2
  • 1
    Show your actual usage. Ideally your post should include a Minimal, Reproducible Example Commented Apr 3, 2020 at 21:34
  • global in the global scope does nothing. Commented Apr 3, 2020 at 21:36

1 Answer 1

1

This is because we can only access the global variable inside the function but for modification you have to use the global keyword inside the function.

For example, this will not give the localbound error:

x = 1 # global variable

def example():
    print(x)
example()

and this will give the error:

For example:

x = 1 # global variable

def example():
    x = x + 2 # increment x by 2
    print(c)
example()

To avoid this error:

x = 0 # global variable

def example():
    global x
    x = x + 2 # increment by 2
    print("Inside add():", x)

example()
print("In main:", x)

Now the answer to the question:

configurationFileInfo_saved = {...}

def configSaved(get, save, saveDict):
    global configurationFileInfo_saved
    if get:
        return configurationFileInfo_saved
    elif save:
        configurationFileInfo_saved = saveDict
Sign up to request clarification or add additional context in comments.

10 Comments

This looks really wrong to me. Can you show how having the global statement outside of the function produces an UnboundLocalError exception?
@jordanm it doesn't. But the OP seems to be using the global statement in the global scope expecting that to mean that a variable is global everywhere. They then "use it in a function", of course, they haven't shown that and they must provide a minimal reproducible example. In which case, likely the OP needs to use a global declaration in the function. Or better yet not rely on global mutable state
@jordanm I have added some examples to address your concern. Please let me know if its still wrong, i will remove the answer and read more about it. :)
The global variable is declared outside of all functions. I meant that I cannot access the variable in any function.
You can access it but cannot modify it.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.