|
|
|
|
|
by sgc
1681 days ago
|
|
Then the function should not use the global variable at all without explicitly indicating it. Printing 2 different variables when referring to a singular one is not great behavior, and passes the smell test for "confusing, untraceable" side effect to me. There should also be a different syntax for initializing a variable and changing a variable value, so that your intention is obvious. I can see the point a bit of added safety of basically defaulting to a constant when declaring global variables, although since I use older languages (but not python), I have not run into that. I do find it humorous that the global variable is semi-protected in python, while the variable type is not. |
|
Generally you use `global` to handle this.
> I do find it humorous that the global variable is semi-protected in python, while the variable type is not.
It's not, there are no differences between them. Bindings (names) are captured like most other languages, and you're free to overwrite them if you wish.
In your example you bind a new string to an existing name. I think it's pretty expected that this wouldn't suddenly change the global object to a local one, that would be madness.
But you're free to update the object _referenced_ if it's mutable: