Usually when we want to make a variable/def/class etc global, we can just declare `global v` at the beginning, but I feel like there could be a better way to do that.
So my idea is to allow doing stuff like
`global a = 3`
or
`nonlocal def x(): ...`
or even
`for global i in []: ...`
in addition to the current `global` statement to allow backwards compatibility What do you think?
Hi Jonathan,
Does this global/nonlocal prefix idea of yours allow us to do anything new that we can't do now?
At first glance, it looks like all it will do is increase the complexity of the language, making it harder for the interpreter and third party tools to identify globals, without any increased functionality.
Is the only benefit here that you save one line of code?
global a a = 3
versus
global a = 3
Also, you should be aware that although CPython doesn't enforce this rule, the language documentation does state that global "must not" be used for loop variables, classes, functions, etc:
https://docs.python.org/3/reference/simple_stmts.html#grammar-token-python-g...
The bottom line is this:
Aside from the saving of one line of code, how would this improve the language?
On Sun, Nov 14, 2021 at 6:05 AM Steven D'Aprano steve@pearwood.info wrote:
global a = 3
The issue i see is that Python doesn't have declarations. So you could use the name, a in multiple places. what would this mean:
a = 34
global a = 0
would it mean that the first a was loca, and the second local? or would it be illegal -- glob could only be used on the first invocation? or ??
and frankly, global (should have) limited use anyway -- making it a bit easier to use seems not very helpful.
-CHB
On Mon, Nov 15, 2021 at 5:05 AM Christopher Barker pythonchb@gmail.com wrote:
On Sun, Nov 14, 2021 at 6:05 AM Steven D'Aprano steve@pearwood.info wrote:
global a = 3
The issue i see is that Python doesn't have declarations. So you could use the name, a in multiple places. what would this mean:
a = 34
global a = 0
would it mean that the first a was loca, and the second local? or would it be illegal -- glob could only be used on the first invocation? or ??
Presumably illegal, same as if you write "a = 34" followed by "global a; a = 0".
and frankly, global (should have) limited use anyway -- making it a bit easier to use seems not very helpful.
Yes, but when you DO need it, you often want to immediately assign. I don't see a lot of times when "global def" would be appropriate, nor "for global i", but a quick grep of my source code shows these:
global last_huffman_tree; last_huffman_tree = root
# Inside a function, load up curses before the first use, but then make it available to helper functions without passing it as a pointless argument global curses; import curses
global total_income; total_income += net
etc. (I've put all these with semicolons for the sake of email but they might just as easily be on two separate lines.)
IMO it would be kinda handy to be able to combine a global statement with an assignment, but it's not a particularly high priority for me. It would reduce a bit of repetition, but that's all.
ChrisA
On 14/11/2021 18:22, Chris Angelico wrote:
IMO it would be kinda handy to be able to combine a global statement with an assignment, but it's not a particularly high priority for me. It would reduce a bit of repetition, but that's all.
Agreed. Sometimes I'd like to be able to write this sort of thing: global TestsRun = 0, ElapsedTime = 0.0 but it's not a big deal. Rob