Hi. I did find that this was brought up once, more than a year ago, but only got one comment, and I believe further discussion may be relevant. I thought to make this as a PEP and as per the tutorial, I write here first.
The concept is to shorthand global and nonlocal along with their first usage in a certain scope.
Suggested syntax:
# without this syntax
g = 0
def fn():
nl = 0
def foo():
global g
g += 1
nonlocal nl
nl += 1
return nl
def set_global(val):
global g
g = val
def read_nonlocal():
nonlocal nl
return nl
use_my_callables(foo, set_global, read_nonlocal)
# with this syntax
g = 0
def fn():
nl = 0
def foo():
global g += 1
nonlocal nl += 1
return nl
def set_global(val):
global g = val
def read_nonlocal():
return nonlocal nl
use_my_callables(foo, set_global, read_nonlocal)
# even shorter - allows concise, short lambdas
g = 0
def fn():
nl = 0
# let's skip foo this time,
# as it can't be shorterned to a lambda
use_my_callables(
# set_global, short lambda,
# using this idea combined with walrus operator
lambda val: (global g := val),
# read_nonlocal, short lambda
lambda: (nonlocal nl)
)
Note that whenever you use the ânonlocalâ or âglobalâ statement in existing Python, youâre (almost?) certainly going to use it for a read and/or write, so it stands to reason that this has use-cases, especially when you need to use callables for arguments (see the case above), for example with asynchronous programming and/or callbacks. This is similar to how C-family languages support declaration and first assignment in the same command (int x = 3;
), which Python, since adopting optional typing, also allows (x: int = 3
) - those are features that allow to unite two parts that often are used together.
Also note how functions whose only purpose is to read and return a nonlocal/global, and functions whose only purpose is to set such variables, can now be written as a lambda, thus making the code shorter and hopefully more understandable - if you have a need for a parameter that is a function that can only be defined as a lambda if this syntax is available, then itâs easier to follow if the function is defined as a lambda, where itâs needed, self-describing by code, rather than by defining an inner def() whose purpose you only see later in the code, and only use once.
While some may note that if you have such a use-case, it may be better solved by just writing classes and using attributes and instance functions (or by other solutions), it also stands to reason that if globals and nonlocals exist as a construct in Python, we may as well make usage for them easier to write and read.
What are your thoughts?
Iâd especially like to hear from you if your opinion falls into one of those:
- I use nonlocals and globals often, and I would use this
- I use nonlocals and globals often, but I wouldnât use this
- I donât use nonlocals and globals, but this would make me more likely to use them
- Iâm opposed to nonlocals and globals, even in quick-and-dirty code, and this would only make people more likely to use these features that I oppose
But whatever your opinion on this, if you have one, do let me know.
Best regards!