s = "Me too."
print(s)
# Global scopes = "I love Geeksforgeeks"
f()
print(s)
This one returns `I love Geeksforgeeks`
Holy mogy, God bless someone doing codereview or inherit python code from some bad developers.
s = "Me too."
print(s)
# Global scopes = "I love Geeksforgeeks"
f()
print(s)
This one returns `I love Geeksforgeeks`
Holy mogy, God bless someone doing codereview or inherit python code from some bad developers.
Me too
I love Geeksforgeeks
The s introduced in f is local to the function unless you use the nonlocal or global keyword with it. To do otherwise seems like a really bad idea to me... def f_global():
global s
s = "Me too."
print(s)
print(s)
f_global()
print(s)
Will print: I love Geeksforgeeks
Me too.
Me too.
Of course... please don't do global mutable variables.I think it was a good choice in the sense that it discourages mutable global state and makes the most common case more concise.
(Some of the older languages do differ, and usually best practices in those languages include running linters that yell at you to use explicit scopes.)
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:
def f():
thing.append(1)
thing = []
f()
print(thing)Fine, we want a test by using `global` to make sure we are doing what we intend when we assign, especially since I see python does not even have true constants so there is a basic lack of safety there. There should also be similar tests before passing every variable with the same name through our function and treating a single name as a list of references without a keyword `all` or similar. Or changing the type of a variable without explicit casting.
To me that lack of continuity just makes a language feel idiomatic and buggy.
I think the example here is misleading and causing confusion.
It's not exactly a language that programmers refer to often, as it's not really general purpose, but I had to do some work with MRI data in it and I hated it mostly due to the scoping.
An "I spent a decent bit of time rewriting the entire lab's codebase in python" type of hate.
Neither are considered pinnacles of language design.
var s = "Geeksforgeeks";
function f_global() {
s = "Me too";
}
// is different from
function f_local() {
var s = "Me too";
}