I have notes from class but I am unsure what is actually happening. Other than adding to confusion, what purpose does shadowing allow on to do? I thought the because globalString is a string type it cannot be modified? How do I access the original value? What is an instance of what in memory terms?
globalList = [1,2,3]
globalString = "global" # can't be modified because it's a string
def updateGlobalString():
global globalString # Does line this do anything?
globalString = "new"
print(globalString)
>>> "global"
updateGlobalString()
>>> "new"
def updateGlobalList():
globalList.append(4)
print(globalList)
>>> [1,2,3]
updateGlobalList()
print(globalList)
>>> [1,2,3,4]
If python Lists are mutable how does this example change the equation when compared to strings? And just to clarify, are any of these values actual Global?
Thank you.
Global means that variable is going to occur in the global space. So that 1st function deletes the old string in global namespace and replaces it with a new one. However, a list is mutable and as a direct result can be changed outside of the global scope.