PEP 640: Unused variable syntax.
One of the problems I have with the Pattern Matching proposal (PEP 622
originally, now PEPs 634, 635, 636) is the special-casing of '_' to not
actually assign to the name, which is a subtle but meaningful divergence
from the rest of Python. In discussions with the authors I proposed using
'?' instead *and* extending that to existing unpacking syntax. The PEP
authors were understandably a little hesitant to tack that onto their
already quite extensive proposal, so I suggested making it a
separate proposal. That proposal is at
https://www.python.org/dev/peps/pep-0640 (unless that hasn't updated yet,
in which case it's at
https://github.com/python/peps/blob/master/pep-0640.rst), and also included
below.
This proposal doesn't necessarily require pattern matching to be accepted
-- the new syntax stands well enough on its own -- but I'm recommending
this *not* be accepted if pattern matching using the same syntax is not
also accepted. The benefit without pattern matching is real but small, and
in my opinion it's not worth the added complexity.
The PEP:
PEP: 640
Title: Unused variable syntax
Author: Thomas Wouters
I can't say that I like the look of pseudo-assignment to question mark: for ? in range(20): ... but I could probably learn to live with it. But one of your rationalisations:
and makes it more obvious that the actual intent is for the value to be unused -- since it is entirely impossible to use it.
is actually an anti-feature, in my opinion. I think that people might like the idea of not actually binding a value in situations like this: a, *?, b = expression until you end up with something unexpected in a and b and need to debug what it going on, either in a debugger or with print: a, *?, b = expression print(?) # wait this doesn't work In my opinion, having a convention to treat certain variables as "unused" is great (I'm partial to `__` myself, to avoid clobbering the special variable `_` in the REPL). But having that be a pseudo-variable which is *actually* unused and unuseable strikes me as being an attractive nuisance. -- Steve
On Tue, Oct 20, 2020 at 11:26 AM Steven D'Aprano
I can't say that I like the look of pseudo-assignment to question mark:
for ? in range(20): ...
but I could probably learn to live with it. But one of your rationalisations:
and makes it more obvious that the actual intent is for the value to be unused -- since it is entirely impossible to use it.
is actually an anti-feature, in my opinion.
I think that people might like the idea of not actually binding a value in situations like this:
a, *?, b = expression
until you end up with something unexpected in a and b and need to debug what it going on, either in a debugger or with print:
a, *?, b = expression print(?) # wait this doesn't work;
I'm not sure how this is different than, say, a, _, _ = range(3) print(_) or a = range(3)[0] print(<whatever the second value is>) That is to say, some things are impossible; if you want to print the value, don't assign to '?'.
In my opinion, having a convention to treat certain variables as "unused" is great (I'm partial to `__` myself, to avoid clobbering the special variable `_` in the REPL). But having that be a pseudo-variable which is *actually* unused and unuseable strikes me as being an attractive nuisance.
And yet that's exactly what is being proposed in pattern matching. https://www.python.org/dev/peps/pep-0634/#id3
-- Steve _______________________________________________ Python-Dev mailing list -- python-dev@python.org To unsubscribe send an email to python-dev-leave@python.org https://mail.python.org/mailman3/lists/python-dev.python.org/ Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/OPUUJWBC... Code of Conduct: http://python.org/psf/codeofconduct/
--
Thomas Wouters
On 20Oct2020 1021, Steven D'Aprano wrote:
In my opinion, having a convention to treat certain variables as "unused" is great (I'm partial to `__` myself, to avoid clobbering the special variable `_` in the REPL). But having that be a pseudo-variable which is *actually* unused and unuseable strikes me as being an attractive nuisance.
I agree entirely. The convention is fine, and the workaround for when you don't want to overwrite a legitimate `_` variable is also fine. Making `_` enough of an official convention (but how?) that linting tools stop warning about it (e.g. type checkers might warn about multiple conflicting assignments) seems like an overall happier path, that neither makes existing code forwards-incompatible nor future code backwards-incompatible. I don't think we have to codify every emergent coding pattern in syntax. Cheers, Steve
On Mon, Oct 19, 2020 at 3:11 PM Thomas Wouters
PEP: 640 Title: Unused variable syntax Author: Thomas Wouters
...
In Python it is somewhat common to need to do an assignment without actually needing the result. Conventionally, people use either ``"_"`` or a name such as ``"unused"`` (or with ``"unused"`` as a prefix) for this. It's most common in *unpacking assignments*::
Many times I'm not using an assignment target, I still like to give a descriptive name. The reason is that it lets me see what value I'm not using. It helps to document and confirm my understanding of the value being unpacked. It also lets you toggle easily between using and not using a value if you're working on the code. To illustrate, I might do this-- scheme, _netloc, _path, params, query, fragment = urlparse(url) instead of this-- scheme, _, _, params, query, fragment = urlparse(url) So I'd prefer if the scheme would allow including a name (either by prefixing or some other method), or at least not preclude such an extension in the future. --Chris
On Tue, Oct 20, 2020 at 2:02 PM Chris Jerdonek
On Mon, Oct 19, 2020 at 3:11 PM Thomas Wouters
wrote: PEP: 640 Title: Unused variable syntax Author: Thomas Wouters
...
In Python it is somewhat common to need to do an assignment without actually needing the result. Conventionally, people use either ``"_"`` or a name such as ``"unused"`` (or with ``"unused"`` as a prefix) for this. It's most common in *unpacking assignments*::
Many times I'm not using an assignment target, I still like to give a descriptive name. The reason is that it lets me see what value I'm not using. It helps to document and confirm my understanding of the value being unpacked. It also lets you toggle easily between using and not using a value if you're working on the code.
To illustrate, I might do this--
scheme, _netloc, _path, params, query, fragment = urlparse(url)
instead of this--
scheme, _, _, params, query, fragment = urlparse(url)
So I'd prefer if the scheme would allow including a name (either by prefixing or some other method), or at least not preclude such an extension in the future.
It does not preclude it -- ?somename is not valid syntax, so it could be added later -- but please note that the pattern matching proposal also does not allow this. Using names instead of ? is still an option -- both in regular unpacking and in pattern matching -- it just does something subtly different. The reason for this PEP is that pattern matching will make '_' (but not any other names) have the behaviour suggested in this PEP, but *only* in pattern matching.
--Chris
--
Thomas Wouters
On Tue, 20 Oct 2020 at 13:13, Thomas Wouters
The reason for this PEP is that pattern matching will make '_' (but not any other names) have the behaviour suggested in this PEP, but *only* in pattern matching.
That's something that should be addressed or debated in the pattern matching PEP. I'm -1 on this PEP being *solely* to patch over a wart in the pattern matching PEP, and the other justifications for the PEP as a standalone proposal don't seem to be convincing people (they don't convince me either, FWIW). Paul
On Tue, Oct 20, 2020 at 2:22 PM Paul Moore
The reason for this PEP is that pattern matching will make '_' (but not any other names) have the behaviour suggested in this PEP, but *only* in
On Tue, 20 Oct 2020 at 13:13, Thomas Wouters
wrote: pattern matching. That's something that should be addressed or debated in the pattern matching PEP. I'm -1 on this PEP being *solely* to patch over a wart in the pattern matching PEP, and the other justifications for the PEP as a standalone proposal don't seem to be convincing people (they don't convince me either, FWIW).
I did say, in the original email:
This proposal doesn't necessarily require pattern matching to be accepted
-- the new syntax stands well enough on its own -- but I'm recommending
this *not* be accepted if pattern matching using the same syntax is not
also accepted. The benefit without pattern matching is real but small, and
in my opinion it's not worth the added complexity.
--
Thomas Wouters
On 20Oct2020 1309, Thomas Wouters wrote:
The reason for this PEP is that pattern matching will make '_' (but not any other names) have the behaviour suggested in this PEP, but *only* in pattern matching.
Then why is this PEP proposing a different syntax? At the very least, wait for pattern matching to get in before proposing an expansion, so then you won't be caught out suggesting the wrong thing :) Cheers, Steve
On Tue, 20 Oct 2020 at 13:25, Thomas Wouters
On Tue, Oct 20, 2020 at 2:22 PM Paul Moore
wrote: On Tue, 20 Oct 2020 at 13:13, Thomas Wouters
wrote: The reason for this PEP is that pattern matching will make '_' (but not any other names) have the behaviour suggested in this PEP, but *only* in pattern matching.
That's something that should be addressed or debated in the pattern matching PEP. I'm -1 on this PEP being *solely* to patch over a wart in the pattern matching PEP, and the other justifications for the PEP as a standalone proposal don't seem to be convincing people (they don't convince me either, FWIW).
I did say, in the original email:
This proposal doesn't necessarily require pattern matching to be accepted -- the new syntax stands well enough on its own -- but I'm recommending this *not* be accepted if pattern matching using the same syntax is not also accepted. The benefit without pattern matching is real but small, and in my opinion it's not worth the added complexity.
Understood. But unless I'm missing something, the pattern matching PEP(s) is/are in limbo at the moment, there's a lot going on in github but nothing has been posted here. So I'm not clear what there is to discuss here at the moment, if the proposal is only relevant if pattern matching includes it, but no published pattern matching PEP has suggested it... (Sorry if the above sounds a little disgruntled, it feels like there's a lot going in "in private" with the pattern matching PEP and I sort of feel like a bit more transparency would be good. Maybe I'm mistaken...) Paul
On Tue, Oct 20, 2020 at 2:44 PM Paul Moore
On Tue, 20 Oct 2020 at 13:25, Thomas Wouters
wrote: On Tue, Oct 20, 2020 at 2:22 PM Paul Moore
wrote: On Tue, 20 Oct 2020 at 13:13, Thomas Wouters
wrote: The reason for this PEP is that pattern matching will make '_' (but
not any other names) have the behaviour suggested in this PEP, but *only* in pattern matching.
That's something that should be addressed or debated in the pattern matching PEP. I'm -1 on this PEP being *solely* to patch over a wart in the pattern matching PEP, and the other justifications for the PEP as a standalone proposal don't seem to be convincing people (they don't convince me either, FWIW).
I did say, in the original email:
This proposal doesn't necessarily require pattern matching to be accepted -- the new syntax stands well enough on its own -- but I'm recommending this *not* be accepted if pattern matching using the same syntax is not also accepted. The benefit without pattern matching is real but small, and in my opinion it's not worth the added complexity.
Understood. But unless I'm missing something, the pattern matching PEP(s) is/are in limbo at the moment, there's a lot going on in github but nothing has been posted here. So I'm not clear what there is to discuss here at the moment, if the proposal is only relevant if pattern matching includes it, but no published pattern matching PEP has suggested it...
They are not in limbo. They are actively being worked on. (At the sprints Brandt mentioned they expect to post updated PEPs later this week.) The Steering Council had a conversation with the PEP authors a while back, discussing various objections and alternatives, including using something else instead of '_'. At that time they were already talking about splitting the PEP up into three parts (which they've since done, but not posted about yet). I'm not sure how to put it differently than I have in the PEP or the email: I proposed they use ? instead of _ and also apply that to regular unpacking (because it is very easy to see pattern matching as an extension of unpacking assignment), and (besides other disagreements) they were uncomfortable including non-pattern-matching proposals in their PEP. This PEP covers the non-pattern-matching uses of '?'. (Sorry if the above sounds a little disgruntled, it feels like there's
a lot going in "in private" with the pattern matching PEP and I sort of feel like a bit more transparency would be good. Maybe I'm mistaken...)
It's not so much 'private' as 'in separate groups', and they're really
still processing all the feedback they've received about PEP 622. There's a
#pattern-matching channel on the discord server used for the core dev
sprints right now (that all sprinters have access to), and the work on PEPs
634, 635 and 636 is happening on the peps repo.
--
Thomas Wouters
On Tue, 20 Oct 2020 at 14:26, Thomas Wouters
I'm not sure how to put it differently than I have in the PEP or the email: I proposed they use ? instead of _ and also apply that to regular unpacking (because it is very easy to see pattern matching as an extension of unpacking assignment), and (besides other disagreements) they were uncomfortable including non-pattern-matching proposals in their PEP. This PEP covers the non-pattern-matching uses of '?'.
OK, fair. In which case, I'll probably wait for the revised pattern matching PEP, but I expect to be -1 on using ? there as well. I was fine with _ and the special rule TBH. Paul
On Tue, Oct 20, 2020 at 01:41:02PM +0200, Thomas Wouters wrote:
a, *?, b = expression print(?) # wait this doesn't work;
I'm not sure how this is different than, say,
a, _, _ = range(3) print(_)
For starters, that will actually print something, not fail with a SyntaxError or runtime exception. You are correct that if I use the same `_` variable in multiple places: a, _, _, b, _ = something and then need to see the "ignore" values for debugging, I can't. I will only see the value in the final `_` unless I rename them. But with your proposal, I can't even see a *single* `?` (non-)target. So that's even more inconvenient than `_` because it affects more cases. - `_` is inconvenient for debugging if you have two or more in one assignment statement; - `?` is inconvenient for debugging if you have two or more in one assignment statement; - PLUS if you have only a single one in the statement. So strictly more inconvenient. Each time I use a `?` target, I expect that eventually I will need to debug that piece of code. When I need to debug it, I will need to change the `?` into a real variable. I don't plan to change it back: why churn the code unnecessarily? So there's this ratchett effect, where over time every `?` I use will eventually be converted into a real variable. So why not just use real variables in the first place? Unless there is a seriously large performance gain from `?` I wouldn't bother using it in the first place. I don't hate the idea, but I don't think it's very useful either.
That is to say, some things are impossible; if you want to print the value, don't assign to '?'.
Indeed, but the problem is that when I name the variable *today*, I might not know that *tomorrow* I will need to see its value.
In my opinion, having a convention to treat certain variables as "unused" is great (I'm partial to `__` myself, to avoid clobbering the special variable `_` in the REPL). But having that be a pseudo-variable which is *actually* unused and unuseable strikes me as being an attractive nuisance.
And yet that's exactly what is being proposed in pattern matching. https://www.python.org/dev/peps/pep-0634/#id3
Quote: "A wildcard pattern always succeeds. It binds no name." That says nothing about variables. It defines a *pattern* which always matches anything. Patterns are a different kind of thing to variables, just as keywords are not variables. A better argument for your position woud be this quote from a different PEP, 635: "The wildcard pattern is a special case of a 'capture' pattern: it accepts any value, but does not bind it to a variable." https://www.python.org/dev/peps/pep-0635/#id2 But I think that description makes a conceptual error. I think it is a mistake to think of the wildcard pattern as a non-binding capture pattern, since that's a contradiction: if it never captures any value, how can it be a capture pattern? I think that it is important to recognise that the wildcard pattern is *distinct* from capture patterns, literal patterns etc. Like literal patterns, it captures nothing. Unlike literal and other patterns, it matches everything. There's a certainly similarity to capture patterns No, that's not "exactly what is being proposed". The *pattern* "_" is not an assignment target. Capture patterns are similar to assigment targets, but patterns in general are not: case [] does not attempt to use [] as an assignment target. Likewise for value patterns such as `case HttpStatus.OK`. Value patterns like HttpStatus.OK are perfectly legal assignment targets, but that's not what they mean inside a case statement. Symbols having multiple meanings are not so unusual: - inside a float, a . is part of the float; - outside of a float, a . is an attribute delimiter; - the chars 'None' have special meaning on their own; - but otherwise inside an identifier, or a string, they're just letters; - round brackets (parentheses) can mean grouping or calling; - square brackets create lists, or subscripting; etc. Why are we so hung up over the fact that inside a case statement, the underscore means something different from outside of a match statement? It seems like a very minor issue to worry about. If we can accept that `None` and other keywords are syntactically legal identifiers, but not actually usable as identifiers as they are reserved for other purposes, then we ought to be able to accept that `_` is syntactically a legal assignment target, but not usable as a capture pattern as it is reserved for another purpose (wildcard pattern). -- Steve
Hello,
On Tue, 20 Oct 2020 00:00:49 +0200
Thomas Wouters
One of the problems I have with the Pattern Matching proposal (PEP 622 originally, now PEPs 634, 635, 636) is the special-casing of '_' to not actually assign to the name, which is a subtle but meaningful divergence from the rest of Python. In discussions with the authors I proposed using '?' instead *and* extending that to existing unpacking
One problem with this PEP, which I didn't see mentioned in the other replies, is that it tries to grab "?" character, which is already sought-for by another pending PEP: "PEP 505 -- None-aware operators", https://www.python.org/dev/peps/pep-0505/ . Use of "?" in PEP640 can be disambiguated enough from PEP505's "??", "?.", "?[" from a compiler token perspective perspective, but what about confusion/clarity to humans: ?, ?, c = a d = b?.c ?, c = b ?? (None, 2) e = b?[i] (And PEP 505 would need to be addressed sooner or later, now that it's part of other mainstream languages. In Python's conceptual debt tower, non-aware operators definitely precede pattern matching). [] -- Best regards, Paul mailto:pmiscml@gmail.com
On 10/20/20 10:45 PM, Paul Sokolovsky wrote:
One problem with this PEP, which I didn't see mentioned in the other replies, is that it tries to grab "?" character, which is already sought-for by another pending PEP: "PEP 505 -- None-aware operators", https://www.python.org/dev/peps/pep-0505/ .
I don't think PEP 505 is much of a concern. It's from 2015, and it's marked "Deferred". I suspect if it was going to happen it would have happened by now. //arry/
participants (7)
-
Chris Jerdonek
-
Larry Hastings
-
Paul Moore
-
Paul Sokolovsky
-
Steve Dower
-
Steven D'Aprano
-
Thomas Wouters