-
Notifications
You must be signed in to change notification settings - Fork 65
Consider alternative wildcards? #92
Comments
As you have pointed out, I agree that The Issue #73 takes away much of the speciality that we initially assigned to One of the arguments brought up against Another question is whether we actually need a special wildcard. Why not just treat |
It also lets the compiler take important shortcuts, such as: match range(HUGE_INT):
case [*_, last]: # No iteration or copying happens here.
... Whatever we choose as a wildcard has to look good in several places:
I think that this is as good a time as any to introduce a
It also has the potential to solve our other sticky issue: load vs. store. Since all of the above are some abstraction of "store", we can denote named stores with case Point(x?, y?): case [start_token, *info?, end_token]: # Mixed load-store example. The breakthrough here is a wildcard just looks like a name capture without... a name! Which is what it is! You'll notice that I prefer it after the name. Let's keep Python reading like English. 😉 |
The thing with That being said, I still feel that a plain
Yes. I fully agree with that. |
At first glance, I sort of agree with you. But I think that those asking for explicitly-marked stores (rather than loads, as it currently stands) make great points. I can see myself being badly bitten by forgetting (or not seeing!) a dot 20 years from now, but I've already trained my brain to see |
In my brief explorations with the prototype implementation I agree -- several times I've found myself writing examples like Still, the only two alternatives available seem to be the current proposal or something using |
I see This is a novel case that, I feel, justifies novel syntax. It's an obvious indicator that something special is happening, but without being intrusive or inconsistent. If Python-Dev responds positively to a simple yes/no on |
I'll also note that |
But why would we want more complex assignment targets? That seems an anti-pattern. |
I don't necessarily think they're a good idea, but allowing us future flexibility is a small positive, in my opinion. |
I am fine with using There are a few reasons why I prefer
|
I updated the PEP with text about * and ...; but of course we still need to deal with the question mark in the room. |
I added a branch showing what expr.py would look like with question mark prefixes for variable bindings. |
@viridia I agree that it should be a prefix. My only main reason of not using |
@Delta456 Consider wildcard in nested patterns. |
@thautwarm |
|
|
Thank you for pointing out the existence of PEP 505, I wasn't aware of that (I had meant to ask if there was a PEP of this kind, since I use this same feature frequently in TypeScript as part of my day job). I don't believe there is any conflict with pep 505 - that proposal uses Stepping back for a moment, I believe what is being proposed is to kill two birds with one stone:
The However, I don't want to start a discussion of named pattern variables here, there is another topic for that: #90 |
Firstly, I think the syntax of PEP 505 is not finally decided yet. Too early to say "no conflicts", unless we make a convincing explanation for this. Secondly, consider ambiguities of the parser is not sufficient, the consistency of semantics is important as well, a good example to show this is the use of If Only when most of the users feel "okay, this is consistent", there is no problem of consistency.
|
By gathering the discussions of this issue and #90, I have a proposal.
Then the syntax could look this way: match value:
case int:
...
case str:
...
case C(!a):
# do stuff with a
case any:
... It looks clarified and concise to me. |
Does ‘case int:’ mean ‘value == int’ or ‘isinstance(value, int)’? Doesn’t feel clarified to me. ;-( In general I think radical rewrites of the proposal are not welcome at this point. We need to decide #90 and then go to the Steering Council. |
Sorry, it's |
@thautwarm I like your votum to consider this not only from the parser's, but first and foremost from the user's perspective. However, this is very much in line what we have been doing here all along 😉.
|
A reasonable compromise could be to have 505's None-aware operator always be ??, rather than using ?? and ? in different contexts. In my view, this creates internal consistency in 505 and makes its usage more obvious in the indexing and access cases (circuit.break?.access.stuff | circuit.break??.access.stuff). In relation to the match/case construct, it seems important to prioritise semantic clarity in a flow control feature over a comparatively niche operator. This is in the case that ? is used in this PEP, for which I think @brandtbucher's update to expr.py in #105 makes a strong case for. |
This seems good to me, but still As |
Or maybe use This meets python's traditional look, and has already built familiar semantics for being wildcard. match xxx:
case C(pass, yyu): ... |
If '?' were to be used here, I can see a way for it to take on the role of a kind of 'query' character across different contexts. Maybe it's clumsy, but it makes sense in my head at the moment.
PEP 505
Typing
EDIT: The intent of this comment is not to suggest an extension or re-work of PEP 622. It is strictly a hypothetical roadmap for a common usage of the '?' character, in response to concerns regarding it being a new special character. |
@ambientnuance, please slow down. Your stream-of-consiousness brainstorming session is way outside the scope of this issue (and even the PEP). It belongs in a blog post, not our issue tracker. I'll repeat Guido's comment here:
EDIT: Thanks for removing the messages. |
I pretty much could see myself writing an class ErrorResponse:
def __init__(self, jsondata):
match jsondata:
case {'status': int(self.status?), 'message': str(self.message?)}:
pass # Or do some extra initialization
case _: raise TypeError() I know that I could capture variables and then assign to self inside the block, but this didn't look that evil to me :) |
No, I don’t want to encourage that. |
I am -1 on making the None-aware operators more verbose. As someone who uses these operators all the time (my day job uses TypeScript), I would not want to add visual clutter to such a useful operator. From a purely parsing standpoint, there is no confusion, because patterns live in a separate parsing context (same as with type hints). |
As I just wrote in the thread for issue #93, it seems like there is basically no mainstream language with support for pattern matching that does not use From what I gathered, the main reason why some people are not comfortable with Given that the wildcard |
Okay, marking as rejected and needs more pep. |
People seem uneasy with our use of
_
as a wildcard. Some proposals that have been made:*
...
?
This seems to be an important thing to either revisit or explain better (and all of these deserve a place in Rejected Ideas at the least).
There are decent arguments against
*
and...
(see one of my responses on python-dev).There is one consideration that makes me look twice at
?
: If the opposition against plain name binding gets stronger, we could propose that to bind a variablex
you have to write?x
, and then plain?
would make a very natural consequence.But even without that, I would say that this does not look so bad:
The text was updated successfully, but these errors were encountered: