What is the difference between a variable that has been scoped using the global keyword in python vs. a globally defined variable?
From my tests there doesn't seem to be any difference. The only real difference I see is that semantically the global keyword can scope a variable to the global level where as globally defining a variable is just that, scoped globally.
My Test:
tmpDict = {}
class functions:
def dictionary(self):
global my_dict
my_dict= {}
global keys_list
keys_list = []
for i in range(3):
my_dict.__setitem__(str(i),i)
tmpDict.__setitem__(str(i),i)
keys_list.append(str(i))
fun = functions()
fun.dictionary()
print(tmpDict)
print(my_dict)
I guess what I'm trying to ask is, If they do the same thing (make a global variable) then why would you ever use the global keyword to do that?