The problem here is the following line:
Lvar = Gvar
Since Gvar
is a list and therefore a complex datatype, Python won't copy the value but instead references to the variable. So whether you modify Lvar
or Gvar
won't make a difference at this point. Instead, you probably want to copy the values which you can achieve in different ways, e.g.:
Lvar = Gvar[:]
Alternatively, you can use the copy module. Check out the Python FAQs for more information on this.
For completion, this is how your modified code would look:
global Gvar
Gvar = ["Hello"]
def someFunction():
Lvar = Gvar[:]
Lvar.append("World")
print(Lvar)
print(Gvar)
someFunction()
As a side note, variable names in Python should be lower-case - upper-case is typically used for class definitions only.