Accessing module level variables, from within a function in the module [duplicate]

Just change the function definition to:

def load():
    global var # this line has been added to the original code
    var = something()

Global variables are read-only from sibling methods. More accurately unless a variable is specified as global, Python consider it as local, but a read access to a local variable name will reach module-level scope if the name is not present in local scope.

See also use of “global” keyword in python and the doc for more details about the global statement

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)