Pattern Matching in Python?

preview_player
Показать описание
Python Enhancement Protocol (PEP) 622 proposes introducing support for structural pattern matching into Python 3.10, much like other functional programming languages like Haskell and ML. It's still a draft, and Python 3.10 is still more than a year away from an official release, but there's a working implementation to try! Here, I take a look at pattern matching in action.

0:00 PEP 622
0:46 Structural Pattern Matching
2:23 Extracting Values from Patterns
5:41 Guards
6:41 Maximum
8:56 Closing

***

Рекомендации по теме
Комментарии
Автор

Incredible, you explain it in an easy to understand and fast way. Congrats

edurz
Автор

LOVE From Nepal .You explain so well Brian.

abhisheksharma
Автор

Very cool
Thank you for taking the time to explain this

immanuelsuleiman
Автор

5:35 terminal bug right? the terminal didn't erase the content of the last TUI application. it happens a lot with me while using pagers (like less or info) etc

yash
Автор

Where can i find the working implementation? I want to try it myself. Great video btw.

khmsalhuas
Автор

Love this! Terminal, vim and python - nothing extra.
Btw anybody knows if Python will optimize tail recursion in this example?

alexeygutikov
Автор

Wow, I didn't realize the robustness of pattern matching in Python. Silly ol me has been using it as a simple switch case.

yepee
Автор

Great video, pattern matching vs regex

kazifaisal-rjjs
Автор

Welcome to the future! Anybody else come here to figure what pattern matching actually was?

forsyth
Автор

7:36 oh, it this the asterisk operator? which expands a list into its elements.
it's called spread in javascript, i don't know its name in python.

yash
Автор

This is what I really want it, thanks you so much

GiniLoh
Автор

+ the useful use of assignment expression. The structural pattern matching feels very natural, except the case for all, which is a _. I hope it will be replaced with a *.

deadeyea
Автор

Your voice sounds like Brian Yu from Harvard CS50, also the way you use Vim.

kitebeachinnbeachinn
Автор

I am pretty skeptical of this feature, especially it's "bells and whistles" that go beyond a simple switch. Switch itself can often be done nicely using a dict literal. E.g.

{404: 'Not found'}.get(status_code)

or if you need something more advanced,

{404:

Combining match with the the walrus operator just makes it more unpleasant to me.

If you want the effect of an if statement, just use an if statement. E.g.

match command:
case ['eat', food]:
if is_animal_product(food):
raise VeganismViolation
deliver_to_mouth(food)
case ['speak', content]:
utter(content)

Don't be breaking animal products into its own case! The purpose of this match statement should be to branch on the type of command, not to handle all branching that the app might do. Again, we are reduced to something that could be handled using the "dict literal as switch" pattern:

op, arg = command
{'eat': handle_eat_command}[op](arg)

allyourcode
Автор

As always with new features in python, let the opinion war begin...

lien
Автор

why is this different than a regular if/elif/else statement?

jmnunezd
Автор

Looks confusing af. Why use bitwise or '|' instead of 'or', and underscore '_' for 'default' when it is typically a variable for unused parameters?

deep.space.
Автор

Without recursive algebraic data types pattern matching is not that powerfull.

DotaMobaUnionRu
Автор

8:45 anyone up for writing a tail-call recursive version of this 😋?

yash
Автор

To complicated structures, I don't want something like this in my production code. Python was slow but simple, now it became slow and complicated like scala, there should be some differences on market elsewhere what is the reason to choose Python?

ssds