Re:Pattern matching accepted for Python
by toxik on Tuesday February 09, 2021 @04:02PM
(#26082342)
Not to mention that expressions after the case keyword have completely different semantics from expressions ANYWHERE else. a | b is not the bitwise or of a with b.
[ Parent
| on HN ]
Re:Pattern matching accepted for Python
by amirkdv on Wednesday February 10, 2021 @12:00AM
(#26085886)
This part is truly strange. This example from the tutorial drove it home for me:
match event.get():
case Click(position=(x, y)):
handle_click_at(x, y)
case KeyPress(key_name="Q") | Quit():
game.quit()
where `Click(position=(x, y))` is not at all what one would expect.What I still don't understand is: does the new syntax magically do this to a user-defined class? i.e. if I say
foo = Click(position=(x, y))
vs ...
case Click(position=(x, y))
those identical expressions, for the same value of Click, have widely different semantics?edit: typo fix
Re:Pattern matching accepted for Python
by bobbylarrybobby on Wednesday February 10, 2021 @09:54PM
(#26097727)
You are (sadly) correct. The latter example checks that the type of `event.get()` is a Click, then that it has a `.position` attribute that be restructured into a two-element tuple `(x,y)`. It's equivalent to `ev = event.get(); if isinstance(ev, Click): (x,y) = ev.position`
You can already define __or__ to do whatever you want!
This is already special-cased in the standard library for, say, set union (where admittedly the concept is more similar to bitwise or).