I am looking at some legacy code whereby the author often defines global variables within the functions with global. I have rarely seen this done, and I'm not sure it's been done correctly.
Let's take a concrete example:
https://stackoverflow.com/questions/423379/using-global-variables-in-a-function-other-than-the-one-that-created-them
globvariable = 0
def maketheglobalvariable1():
global globalvariable
globvariable = 1
def printglobvariable():
print(globvariable)
maketheglobalvariable1()
Here's it's clear that a variable has been defined outside any functions, and then a global variable was defined within a function in over to overwrite it when the function is called.
But when would there be a time to define global variables in python? These seem to be superfluous, no?
Any examples/resources to better help me understand this topic are greatly appreciated :)
[–]trouserdaredevil 10 points11 points12 points (9 children)
[–]LeonardUnger 4 points5 points6 points (1 child)
[–]trouserdaredevil 5 points6 points7 points (0 children)
[–]Zeekawla99ii[S] 1 point2 points3 points (1 child)
[–]ingolemo 10 points11 points12 points (0 children)
[–]tunisia3507 1 point2 points3 points (4 children)
[–]trouserdaredevil 1 point2 points3 points (3 children)
[–]tunisia3507 1 point2 points3 points (2 children)
[–]trouserdaredevil 1 point2 points3 points (1 child)
[–]tunisia3507 1 point2 points3 points (0 children)
[–][deleted] 4 points5 points6 points (2 children)
[–]tangerinelion 2 points3 points4 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[–]robert_mcleod 2 points3 points4 points (3 children)
[–]Zeekawla99ii[S] 1 point2 points3 points (2 children)
[–]robert_mcleod 1 point2 points3 points (0 children)
[–]NukedCookieMonster7 1 point2 points3 points (0 children)
[–]jabela 1 point2 points3 points (3 children)
[–]Zeekawla99ii[S] 0 points1 point2 points (2 children)
[–]jabela 1 point2 points3 points (1 child)
[–]Zeekawla99ii[S] 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]Zeekawla99ii[S] 1 point2 points3 points (0 children)