Hello everyone, this is my first crack at commenting on a PEP, so apologies for
mistaking any developer colloquialisms, or if this is the wrong channel to go
through.
In a nutshell, I was mulling over my initial difficulty in understanding name
patterns and had the thought of ‘declaring’ so-called ‘placeholder’ variables
at the beginning of the match block, so that the reader is ‘primed’ for their
use throughout:
x_literal = a_value
y_literal = another_value
match point:
proxy x, y
case (x_literal, y):
print(f”Y={y} at literal X”)
case (x, y_literal):
print(f”X={x} at literal Y”)
case (x_literal, y_literal):
print(“That is the literal point”)
case (x, y):
print(f”Arbitrary point: X={x}, Y={y}”)
case _:
raise ValueError(“Not a point”)
The use of ‘proxy’ instead of ‘placeholder’ is simply an attempt at brevity
without diverging substantially from the meaning.
The use of a placeholder variable reminded me a lot of comprehensions, which
are explicit in where placeholders come from, but only towards the end of the
comprehension:
y = [f(a, b) for a, b in iterable]
A contrasting example is the lambda function, which ‘declares’ its placeholders
upfront:
y = map(lambda x, y: f(x, y), iterable)
In reading these two examples left to right, I believe that it is easier to
understand the lambda function in a first pass, whereas the list comprehension
often requires looking back at the expression after reading the ‘for’ clause at
the end. This difference in first-pass understanding becomes more apparent when
the expression or function is more complex. In this vein, ‘declaring’
placeholder variables upfront makes sense to me when the context in which they
can be used is (relatively) large and not self-contained.
Now, this does add “new syntax”, which is addressed in the ‘Alternatives for
constant value pattern’ section of the PEP; but in reading this thread, it
seems like such a solution remains appealing. For myself, a one-off
‘declaration’ with a reasonably unambiguous keyword like proxy makes the code
easier to follow as one reads through it, without the need for variables to be
typed out differently (consistent with all other variable usage).
I reckon something like this idea would improve comprehensibility (not just raw
readability) and is more in line with other prose-like constructs in Python
(try/except, with, and/or). This is particularly important for people that are
new to either this feature (everyone at first), or the language as a whole.
Not being all too familiar with CPython implementation details myself, my idea
is certainly open to technical critique (and of course qualitative
impressions). As a bit of a stab in the dark, ‘proxy’ could be a soft keyword
within a match block, similar to ‘match’ being a soft global keyword.
_______________________________________________
Python-Dev mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/FRC5ZNXQPWWA4D2SJM4TYWMN5VALD3O6/
Code of Conduct: http://python.org/psf/codeofconduct/