Mutating a global variable from within a function

global-variables, python

Solution

You can use the `global` keyword which means you'll be modifying the global variable `someVar`:

someVar = "none"

def someFunction():
    global someVar
    if whatever is whatever:
        someVar = "somethingElse"
    return someVar

Problem

If I start a script with something like: ``` someVar = "none" def someFunction(): if whatever is whatever: someVar = "somethingElse" return someVar ``` Then, in a different function I check someVar, will someVar be "none" or "somethingElse"? I hoped "somethingElse", but instead, having written a function that sets someVar to something else, I'm finding that someVar remains "none". Is that expected? If you manipulate a global variable fromw ithin a function does the global variable change going forwards?

Original source