python - Assigning an argument to a global variable with the same name -


how can assign function argument global variable exact same name?

note: can't self.myvariable = myvariable because function not inside of class.

when write following code, error saying "argument both local , global."

myvar = 1 def myfunction(myvar):     global myvar 

is impossible? , if uncommon in other languages? coming java i'm used this.myvar = myvar

edit know can rename variable. that's easy way out.

best solution: refactor code. rename something, or use object object properties instead of global variable.

alternative, hacky solution: modify global variable dict directly:

my_var = 1 def my_function(my_var):     globals()['my_var'] = my_var 

Comments