A Closer Look At Structural Pattern Matching // New In Python 3.10!

แชร์
ฝัง
  • เผยแพร่เมื่อ 13 มิ.ย. 2024
  • In this video, I show an example of structural pattern matching, a pretty powerful feature that's part of Python 3.10. There are a couple of really nice things you can do with this, and although it resembles the traditional switch statement, it goes far beyond what the 'wicked switch' can do.
    The code I worked on in this video is available here: github.com/ArjanCodes/2021-st....
    💡 Here's my FREE 7-step guide to help you consistently design great software: arjancodes.com/designguide.
    🎓 Courses:
    The Software Designer Mindset: www.arjancodes.com/mindset
    The Software Designer Mindset Team Packages: www.arjancodes.com/sas
    The Software Architect Mindset: Pre-register now! www.arjancodes.com/architect
    Next Level Python: Become a Python Expert: www.arjancodes.com/next-level...
    The 30-Day Design Challenge: www.arjancodes.com/30ddc
    🛒 GEAR & RECOMMENDED BOOKS: kit.co/arjancodes.
    💬 Join my Discord server here: discord.arjan.codes
    🐦Twitter: / arjancodes
    🌍LinkedIn: / arjancodes
    🕵Facebook: / arjancodes
    👀 Channel code reviewer board:
    - Ryan Laursen
    - Sybren A. Stüvel
    🔖 Chapters:
    0:00 Intro
    1:02 Explaining the example
    1:35 Simple pattern match example
    3:37 A more elaborate match example
    7:49 Match with conditions
    9:30 Match with objects
    14:42 Caveats of using structural pattern matching
    16:11 Other Python 3.10 features & final thoughts
    #arjancodes #softwaredesign #python
    DISCLAIMER - The links in this description might be affiliate links. If you purchase a product or service through one of those links, I may receive a small commission. There is no additional charge to you. Thanks for supporting my channel so I can continue to provide you with free content each week!

ความคิดเห็น • 184

  • @ArjanCodes
    @ArjanCodes  2 ปีที่แล้ว +91

    Too bad my camera decided to switch off near the end. It probably couldn't handle the awesomeness of structural pattern matching 😎.

    • @cefk9944
      @cefk9944 2 ปีที่แล้ว +1

      First off:
      Thank you for your videos .. Keep them coming!
      You taught me a lot, already and I like your style.
      Constructive input:
      The "more space for displaying the code"-thing could be even more positive, if you increased the font-size.
      I can't watch your videos from the couch.
      I have to sit directly in front of the monitor (fine for a "type-along and we'll build e.g. a snake-game"-type of video .. but for binge-watching "I'll explain a concept or new language-feature"-videos, I'd prefer to be lazy and watch from the couch (as we all know: lazy === good).
      Or maybe you could zoom in/out, depending on what makes sense at that moment .. e.g.:
      - Very big font for looking at the currently interesting few lines of code ..
      - Zoom out, until all the relevant code is visible, whenever the context is needed.
      I'll continue watching your videos, whatever you decide to do/not to do .. and I don't know, what other viewers think about this .. it's just my opinion.
      Cheers :)

    • @MichaelSchellerwayne
      @MichaelSchellerwayne ปีที่แล้ว

      Arjan > Netflix

  • @dovids.greenberger435
    @dovids.greenberger435 2 ปีที่แล้ว +69

    Idea for a future series:
    Go through a module, for example pathlib, and breakdown the inner workings. There is so much to learn from the professionals who write production grade code.

  • @maxwelllindsay8591
    @maxwelllindsay8591 2 ปีที่แล้ว +89

    Commenting for the algorithm. These are seriously some of the most
    useful programming tutorials I've seen. The market on youtube is crowded
    but I think you're headed to the top due to the excellent content and production quality. Keep it up!!

    • @ArjanCodes
      @ArjanCodes  2 ปีที่แล้ว +4

      Thank you Maxwell, I’m happy the videos are helpful to you. Let’s see where the channel goes in the coming months :).

  • @randolphbusch7777
    @randolphbusch7777 2 ปีที่แล้ว +25

    10:35 since you're explicitly working with Pyton 3.10, you can just use list[str] instead of the typing import. Same with dict, tuple, set etc.

  • @petoknm
    @petoknm 2 ปีที่แล้ว +43

    typing.List[T] => list[T] since python 3.9

    • @ArjanCodes
      @ArjanCodes  2 ปีที่แล้ว +13

      Yes, realized that too. I’ll use the built-in types for my next examples.

  • @TheJobCompany
    @TheJobCompany 2 ปีที่แล้ว +51

    Honestly, love this addition to the language. Originating from C and being a procedural programming junkie myself, I missed the equivalent of a switch statement in Python and dreaded writing long-winded if-elif-else statements, so I'll def be using match a lot

    • @kickflipz
      @kickflipz 2 ปีที่แล้ว

      I'm new to switch and match but Im going to start only using them to get practice

    •  2 ปีที่แล้ว +14

      Before this, I often used dictionaries to map from some string (like the command name) to a function to call. Usually this leads to quite a nice design, especially considering that you can build that dictionary in various ways. You can make classes register which command they should be instantiated for, for example.

    • @TheJobCompany
      @TheJobCompany 2 ปีที่แล้ว +4

      @ Hey, Dr. Stüvel! Big fan of your work! I have also used dictionaries in this way before and I can say, in some cases it produces way cleaner code. For example, often in C, I would use a switch to create a mapping between one value to another and all I would have in a case's body would be an assignment expression, followed by a break statement. That can be written way cleanly in Python with the use of a dictionary and I still wouldn't refactor that as a match whenever I upgrade to 3.10.
      However cases where the dict maps to functions, it kinda feels like a hack, especially whenever some functions take different parameters and I have to wrap them with lambdas, it kinda reminds me of how some JavaScript APIs abuse the use of callbacks and I don't really like it

    • @con_keister_door
      @con_keister_door 2 ปีที่แล้ว +1

      You took the words out of my mouth @THE JOB COMPANY

    • @doraicharansimhamuthineni8049
      @doraicharansimhamuthineni8049 2 ปีที่แล้ว +1

      @@TheJobCompany totally agree with you

  • @samuel.stelzer
    @samuel.stelzer 2 ปีที่แล้ว

    I skipped this video when it came out, because I wanted to watch it when I start using Python 3.10. But somehow TH-cam stopped recommending me your videos (have been watching one every day for the last weeks) - so I decided to go to your channel and browse the videos by myself. Turns out: this is the only one I haven't watched yet. Great job!

  • @chrisweldon791
    @chrisweldon791 ปีที่แล้ว

    I have learned more from 10 arjan videos that i have from any other source on python. another great video this.

    • @ArjanCodes
      @ArjanCodes  ปีที่แล้ว

      Thanks so much, Chris glad the content is helpful!

  • @iChrisBirch
    @iChrisBirch 2 ปีที่แล้ว

    Another great video. Thank you for so many examples in a series to show the features and always appreciate the high audio/video production quality of your videos and I love your choice of background and lights.

    • @ArjanCodes
      @ArjanCodes  2 ปีที่แล้ว

      Thank you, I'm happy you enjoy the videos!

  • @CaptainCsaba
    @CaptainCsaba 2 ปีที่แล้ว +3

    The amount of work goint into these videos is astounding. I am so happy that I have found your channel, there are so many ways one can write cleaner code but it's always explained a not easy to understand way, unlike here. Thank you for your awesome videos!

    • @ArjanCodes
      @ArjanCodes  2 ปีที่แล้ว +1

      Thank you, glad you like the videos!

  • @goktugbasaran7591
    @goktugbasaran7591 2 ปีที่แล้ว

    Arjan I recently discovered your channel and honestly your way of expressing whatever the idea on your mind is very precise and neat. Me and my friends also have a channel for educational content mostly on software and your channel really re-inspired me. I have spent lots and lots of hours on TH-cam trying to find good quality and fun content on topics like these and you are golden. Thank you. Can't wait to see more of your content!

    • @ArjanCodes
      @ArjanCodes  2 ปีที่แล้ว +1

      Thank you so much! I'm happy you're enjoying the content!

  • @douglasfrb
    @douglasfrb 2 ปีที่แล้ว +10

    This new version is looking promising! I've been waiting for this feature for so long. 😂
    I appreciate your content, man. Keep up the good work!

    • @ArjanCodes
      @ArjanCodes  2 ปีที่แล้ว +1

      Thanks Douglas, glad you like it!

    • @AWriterWandering
      @AWriterWandering 2 ปีที่แล้ว +1

      Yeah, this is such a fundamental feature that python has been lacking.

  • @luismiguel5391
    @luismiguel5391 2 ปีที่แล้ว

    Switch case in python is something that I've been waiting for so long. Your tutorials are amazing by the way.

  • @hicoop
    @hicoop 2 ปีที่แล้ว +3

    Thank you so much for the explanation! Structural Pattern Matching is more powerful than I imagined and I can't wait to try it out!

    • @ArjanCodes
      @ArjanCodes  2 ปีที่แล้ว +1

      You are so welcome!

  • @AnAmiC
    @AnAmiC 2 ปีที่แล้ว +6

    Rust programmer: welcome to the club! This is an amazing language feature.

    • @ArjanCodes
      @ArjanCodes  2 ปีที่แล้ว +1

      Agreed!

    • @AWriterWandering
      @AWriterWandering 2 ปีที่แล้ว

      Swift programmer: ditto

    • @AssemblyWizard
      @AssemblyWizard 2 ปีที่แล้ว +2

      Brainfuck programmer: guys what's this 'if' I keep hearing about?

  • @zacky7862
    @zacky7862 2 ปีที่แล้ว

    The best structural pattern matching tutorial I've watched.

  • @AndreasFalk
    @AndreasFalk 2 ปีที่แล้ว +1

    Your content is great, keep up the good work!
    I just love to watch your videos when eating breakfast or just chilling after a hectic workday.

    • @ArjanCodes
      @ArjanCodes  2 ปีที่แล้ว +2

      A suitable breakfast item would be (fruity) loops ;).

  • @mrc1500
    @mrc1500 2 ปีที่แล้ว +3

    Just starting to master structural pattern-matching in Scala and it's good to see it coming to Python 3. Loving your videos.

    • @ArjanCodes
      @ArjanCodes  2 ปีที่แล้ว

      Thanks so much, Iain!

  • @patricktarnaud
    @patricktarnaud 2 ปีที่แล้ว

    your videos are always very interesting. thank you !

  • @jindy94
    @jindy94 2 ปีที่แล้ว

    Wished I have this channel so much earlier in my career!!

  • @dovids.greenberger435
    @dovids.greenberger435 2 ปีที่แล้ว +1

    Thank you!! Beautifully explained. You are a gifted teacher. I previously watched another video on this topic and was baffled. After your video I feel primed for the 3.10 release.

    • @ArjanCodes
      @ArjanCodes  2 ปีที่แล้ว

      Glad it was helpful, Dovid!

  • @noomade
    @noomade 2 ปีที่แล้ว +8

    Haskell is just amazing!

    • @ArjanCodes
      @ArjanCodes  2 ปีที่แล้ว +2

      Yeah, it’s pretty inspiring ;)

  • @thefrator5275
    @thefrator5275 2 ปีที่แล้ว

    I've been enjoying your library of videos! Keep it up

    • @ArjanCodes
      @ArjanCodes  2 ปีที่แล้ว

      Thanks, will do, Nicholas!

  • @1lugarparapensar
    @1lugarparapensar 2 ปีที่แล้ว

    I just came across your channel for first time and I LOVE IT!!! Thank you so much for your videos!
    I'm a Python Developer too, who is learning Scala and I'm also a TH-camr 😂 So Hi Five for all our matching !!! 😂
    saludos from Argentina 🥰

  • @anjanavabiswas8835
    @anjanavabiswas8835 2 ปีที่แล้ว

    Finally a switch case ... My day is saved. I always used an external switch package as a standin

  • @SP-db6sh
    @SP-db6sh ปีที่แล้ว

    Best tutorials on Structural pattern matching....
    Thanks 🙏🙏🙏
    After this tut, dev will use it for complex it-else,
    Waiting for V2 of it, with regex & using async task group on functions part of each case

  • @mateuszbaginski5075
    @mateuszbaginski5075 2 ปีที่แล้ว

    Thanks, man! Very clearly explained!

  • @presstv
    @presstv ปีที่แล้ว

    great job Arjan

  • @bayuganspride
    @bayuganspride ปีที่แล้ว

    Great content. Very helpful for new Python programmers.

  • @hudheyfa
    @hudheyfa 2 ปีที่แล้ว

    much appreciated how you explain things!! Thanks again.

    • @ArjanCodes
      @ArjanCodes  2 ปีที่แล้ว

      My pleasure! Glad you liked it!

  • @FalseDev
    @FalseDev 2 ปีที่แล้ว +1

    Quite a useful new feature, can't wait to try it out!

    • @ArjanCodes
      @ArjanCodes  2 ปีที่แล้ว

      Agreed! Same here :)

  • @HadesMrDark
    @HadesMrDark 2 ปีที่แล้ว

    Thank you so much for the video, it was absolutely flawless.

    • @ArjanCodes
      @ArjanCodes  2 ปีที่แล้ว

      Glad you enjoyed it!

  • @NostraDavid2
    @NostraDavid2 2 ปีที่แล้ว

    This looks amazing for beginners that want to be able to easily parse input, or perhaps even files (by looping over lines).

  • @user-tg3tg9gh3q
    @user-tg3tg9gh3q ปีที่แล้ว

    Very nice feature. Thank you

    • @ArjanCodes
      @ArjanCodes  ปีที่แล้ว

      Thank You, Glad you like it.

  • @johncongerton7046
    @johncongerton7046 7 หลายเดือนก่อน

    decidedly the best tutor on TH-cam

    • @ArjanCodes
      @ArjanCodes  7 หลายเดือนก่อน

      I appreciate the kind words, John! Thank you :)

  • @akshaysharma6160
    @akshaysharma6160 2 ปีที่แล้ว +1

    Hi 👋... Your videos are really good and very smooth . Can you please make a video on Mixin classes and when to use them . Thanks 👍

  • @profsor5008
    @profsor5008 2 ปีที่แล้ว

    finally switch command in python.. one of the most comfortable programming languages without switch sounded bad. Thanks for this tutorial and to see you in the next one ;)

  • @dudaseifert
    @dudaseifert 2 ปีที่แล้ว

    Your videos are incredoble bro, nice job

    • @ArjanCodes
      @ArjanCodes  2 ปีที่แล้ว

      Glad you like them, Eduardo!

  • @aurelistrate1633
    @aurelistrate1633 2 ปีที่แล้ว

    Great video as always!

    • @ArjanCodes
      @ArjanCodes  2 ปีที่แล้ว

      Glad you liked it!

  • @immortalsun
    @immortalsun ปีที่แล้ว

    Fantastic video!

  • @lianyf8866
    @lianyf8866 2 ปีที่แล้ว

    I just studied functional paradigma applyin it to oCaml and i love this new feature

  • @edgecrush3r
    @edgecrush3r ปีที่แล้ว

    what a great implementation of the classical Switch statement offering much versatility. As i am not a big fan of the argparse module, this certainly makes my life easier in the future.

  • @sk00sha18
    @sk00sha18 2 ปีที่แล้ว

    Keep up the good work, I appreciate the effort :)

    • @ArjanCodes
      @ArjanCodes  2 ปีที่แล้ว

      Much appreciated!

  • @JSDudeca
    @JSDudeca 7 หลายเดือนก่อน

    Great video. One huge opportunity is to follow the functional programming patterns of returning tuples of values from functions and matching results. Eg. function `def do_foo()` returns (ok: bool, reason: str, data: dict) . By using structural pattern matching you can write cleaner and intuitive invocation code. Once I get my code base moved up to 3.10+, I'll be using this heavily. T hanks for the great videos!

  • @RoamingAdhocrat
    @RoamingAdhocrat 2 ปีที่แล้ว +1

    alternative: a dict where the keys are commands and the values are function pointers which handle the rest of the input. As demonstrated beautifully in Mike Pirnat's "Dungeons & Dragons & Python", one of my favourite conference talks
    "quit" and "exit" and "bye" all invoke the same function, in its own submodule elsewhere, which decides how to deal with "--force" or "-f" etc

    • @peter9477
      @peter9477 2 ปีที่แล้ว

      That is the Pythonic way to do a simple switch. The new structural pattern matching is meant for more complex situations.

  • @oktay9812
    @oktay9812 2 ปีที่แล้ว +3

    Commenting for TH-cam algorithm. Greate content!

  • @happygarlic13
    @happygarlic13 ปีที่แล้ว

    cool video thx, maybe a side by side comparison of the 3.10 and the previous version would be nice.

  • @TBaguette
    @TBaguette 2 ปีที่แล้ว

    Having tried Rust after Python, it's a feature I was missing. Glad to see it coming to Python as well.

  • @robertbrummayer4908
    @robertbrummayer4908 2 ปีที่แล้ว

    Woooohooooo, anotther great video! However, I am not quite sure how much I will actually use this feature, let's see.

  • @Tekay37
    @Tekay37 2 ปีที่แล้ว +1

    I've learned that switch/case statements have their place in factory methods where different inputs lead to the creation of different classes. I think that's where this tool might become useful as well because you don't depend on switching over a primitive value like a string or an int.

  • @WernerRaath
    @WernerRaath 2 ปีที่แล้ว

    I think the maintainers of argsparse are going to be very happy with this. Big refactor though!

  • @mistercakes
    @mistercakes 2 ปีที่แล้ว

    Thanks for this.

  • @imadetheuniverse4fun
    @imadetheuniverse4fun 2 ปีที่แล้ว +1

    Very interesting. For the object pattern matching, is it actually instantiating a new object for every case check? Wondering whether this would have performance implications for classes with expensive constructors.

  • @neilhiggins350
    @neilhiggins350 หลายเดือนก่อน

    This was really helpful. How about a special on how to use it for minimal unique string matching when parsing commands and arguments, e.g. A - A*dd, Beg - Beg*in, Bef - Bef*ore, etc.

  • @ManuelOchoa2711
    @ManuelOchoa2711 ปีที่แล้ว

    ¡Gracias!

    • @ArjanCodes
      @ArjanCodes  ปีที่แล้ว +1

      Thank you very much! Glad you liked the video.

  • @mithunkinarullathil643
    @mithunkinarullathil643 2 ปีที่แล้ว

    Great tutorial @arjan. One question. In your first example, you used None to mention the return type of the methods without any return. Is this the standard/preferred way? I don't really mention it if there is no return. Thank you.

  • @MarioAndresYepesCifuentes
    @MarioAndresYepesCifuentes 2 ปีที่แล้ว

    This reminds me of Elixir pattern matching, very cool!. BTW How about a comparison between `pyenv`, `pipenv` and `python -m venv`?

  • @Maric18
    @Maric18 2 ปีที่แล้ว

    i already have router functions that basically do this with nested ifs and calling other functions, this will clean that up nicely :D

  • @EW-mb1ih
    @EW-mb1ih 2 ปีที่แล้ว

    Nice video. Don't you think adding OOP in the "run_command_v4 function is a bit overkill?

  • @ChristOfSteel
    @ChristOfSteel 2 ปีที่แล้ว +7

    Hi, as someone who writes both a lot of Haskell and a lot of Python code, this is super interesting. One question, in run_command_v4 function, in the `--force` case, your code matches only arguments, that start with `--force` or `-f`. In the other versions the order did not matter. Is there a way to compare fields of objects not only by equality, but by any boolean construct (like `in`), as you did in the run_command_v3?
    Anyway super interesting, thanks for showing us :)

    • @ArjanCodes
      @ArjanCodes  2 ปีที่แล้ว +3

      You can add a condition to a case when you're matching objects, like so:
      case Command(command="quit" | "exit" | "bye") if "--force" in command.arguments or "-f" in command.arguments:
      ...
      I don't think this is very readable though. It would be nice to have the possibility to write something like "--force" | "-f" in arguments in the case itself.

  • @obed818
    @obed818 2 ปีที่แล้ว

    Neat! One more question please, i think you didn’t said it explicitly; okay the case has an order importance, but if i dont qui the program as you do with quit() , may i go by many case or only one is possible as the elif statement ? Thanks i hope i could currently use python 3.10 its dope!

  • @joaopedrorocha5693
    @joaopedrorocha5693 8 หลายเดือนก่อน

    Neat!

  • @edgecrush3r
    @edgecrush3r ปีที่แล้ว

    Thanks!

    • @ArjanCodes
      @ArjanCodes  ปีที่แล้ว

      Thank you so much, Tony!

    • @edgecrush3r
      @edgecrush3r ปีที่แล้ว

      Graag gedaan Arjan! Echt top dat je naast nieuwe Python features ook focussed op klassieke Design Patterns en andere boeken van oa Gang of Four. Keep up the great content! You Rock!

  • @1234admir
    @1234admir 2 ปีที่แล้ว

    Learned Scala 5ish years ago and starred working with it, since then I get deja vu every time I see a new feature of another language being released.

    • @ArjanCodes
      @ArjanCodes  2 ปีที่แล้ว

      Yeah, programming languages definitely get inspired by other languages all the time. (which is a good thing actually)

    • @1234admir
      @1234admir 2 ปีที่แล้ว

      @@ArjanCodes Of course, I am just saying that Scala was quite the inspiration to many of them and doesn't always get the credit it deserves.

  • @cherryramatis2508
    @cherryramatis2508 2 ปีที่แล้ว +1

    You are the best content creator for programming, love your channel
    Cheers from Brazil

    • @ArjanCodes
      @ArjanCodes  2 ปีที่แล้ว +1

      Glad you enjoy it!

  • @andreaardemagni6401
    @andreaardemagni6401 2 ปีที่แล้ว

    Hi! I have just been introduced to your channel and I wanted to ask - how shall I navigate your videos from more like beginner level to advanced? Do you actually have videos for python beginners? Thank you!

  • @JJSogaard
    @JJSogaard 2 ปีที่แล้ว

    This is pure magic… especially when objects get involved.

    • @ArjanCodes
      @ArjanCodes  2 ปีที่แล้ว

      Yeah... I'm curious to see what use cases people come up with for this feature.

  • @sagnikbhattacharya1202
    @sagnikbhattacharya1202 2 ปีที่แล้ว +1

    Just patiently waiting for Python to turn into Easy Haskell

  • @torrentails
    @torrentails 2 ปีที่แล้ว

    *Camera accidentally switches off*
    Arjan: Sorry for the camera, I'll try to put a positive spin on it.
    Me: Finally! Me and my ADHD brain can actually focus on the code being written, instead of constantly getting distracted!

  • @PaulJohnsonM
    @PaulJohnsonM 2 ปีที่แล้ว

    Would it make sense to test for something like "quit" with the simple case and then put the rest of the complexity into that case, possibly by switching again? It seems like this would spread the complexity out in a more orderly way and make it easier to read.

  • @gedtoon6451
    @gedtoon6451 ปีที่แล้ว

    At 15:00 you mention that the order is important. This also applies to a if-elif-else construct.
    Also I think you need some structural hair gel :)

    • @ArjanCodes
      @ArjanCodes  ปีที่แล้ว

      hahhaha! I agree :)

  • @john-r-edge
    @john-r-edge 2 ปีที่แล้ว

    Question. at 9m34 looking at version 3 of the command. Comparing the "with --force" case with the "no force", the print statements are different, but the next command is quit() in both cases. Not seeing how the behaviour will actually be different.

  • @xpmon
    @xpmon ปีที่แล้ว

    Can you match by type and tuple, sorta like Scala's match?

  • @snoopyjc
    @snoopyjc 2 ปีที่แล้ว +1

    Can you match a REGEX with this?

  • @baphnie
    @baphnie ปีที่แล้ว

    Can someone explain how the pipe character is used in python? It seems like Arjan is using it here as a logical OR, but I can't seem to get that functionality out of it in other settings.

  • @joaquimbento9406
    @joaquimbento9406 6 หลายเดือนก่อน

    But is'nt it slower than if/elif/else? Or should I just use polars/panda/numpy for the heavy duty computing?

  • @tomasbernardo5972
    @tomasbernardo5972 ปีที่แล้ว

    In the first example why did you use the ! in the "other" case?

  • @thesupercoach
    @thesupercoach 2 ปีที่แล้ว

    In v4, why did you use command as the variable for the match statement along with command being an element of the Command class? Doesn't overuse of the word command like that muddy the water for anyone reading it?

  • @Schecterbaby
    @Schecterbaby ปีที่แล้ว

    @13:30 why use an array for the arguments? Is that just personal preference?

  • @GlobusTheGreat
    @GlobusTheGreat 2 ปีที่แล้ว +8

    Wait, has python not had a switch statement equivalent for all this time and I never noticed? lol

    • @zolika154
      @zolika154 2 ปีที่แล้ว +1

      nope :D

    • @MrBanarium
      @MrBanarium 2 ปีที่แล้ว +4

      Nope. The usual argument is that a switch doesn’t provide anything new compared to a simple if-elif-else chain. Structural pattern matching, however, is way more powerful than a simple switch, enough to justify adding it to the language.

    • @MattRose30000
      @MattRose30000 2 ปีที่แล้ว +2

      The closest Python had was that you used a dictionary, with the keys being tha cases, and lambda functions as values.

  • @zachkramer4347
    @zachkramer4347 ปีที่แล้ว

    Can this be used for factory pattern?

  • @josesimoes3478
    @josesimoes3478 2 ปีที่แล้ว

    I wonder, how does the object based matching affect performance/memory usage compared to the other alternatives?

    • @ArjanCodes
      @ArjanCodes  2 ปีที่แล้ว

      Haven't tested that, but it's an interesting question. I don't expect the performance hit to be that big though, since accessing instance variables in an object should be pretty fast.

  • @digitig
    @digitig ปีที่แล้ว

    What's that "other!r"? I don't recognise that use of the exclamation mark or what the r does.

  • @falxie_
    @falxie_ 2 ปีที่แล้ว

    Oh wow I really want JavaScript to have this too

  • @Jarek.
    @Jarek. 2 ปีที่แล้ว

    8:58 I'm not a big fan of v3 with two "case" clauses for handling same "quit" | "exit" | "bye" - repetition which could be error prone if you add "f*k off" as a 4th exit option but forgot to update all cause lines. This might be especially painful when you have more "--" options.

  • @TheBigonner
    @TheBigonner 2 ปีที่แล้ว +1

    Should i just stop bothering with learning if/else statements then?

    • @ArjanCodes
      @ArjanCodes  2 ปีที่แล้ว

      I don’t think this will replace if-else statements. For simple branching by checking a condition, if-else is shorter than structural pattern matching.

  • @valizeth4073
    @valizeth4073 ปีที่แล้ว

    What does the !r do in the string interpolation?

    • @IgorKim-Alyona
      @IgorKim-Alyona ปีที่แล้ว

      Was asking the same... docs.python.org/3/library/string.html#grammar-token-format-string-conversion. It's a conversion flag: r=repr(), s=str(), a=ascii().

  • @CreativePuppyYT
    @CreativePuppyYT 2 ปีที่แล้ว

    Is this the new best way to keep track of states in an application?

    • @peter9477
      @peter9477 2 ปีที่แล้ว

      Probably not. Orthogonal concept.

  • @RoamingAdhocrat
    @RoamingAdhocrat 2 ปีที่แล้ว +1

    "Structural Matter Patching", eh
    I need one of those signs to be behind me in Teams calls

  • @WeisSchwarz
    @WeisSchwarz 2 ปีที่แล้ว

    I'm pretty new to this channel. Is that some inside joke you put on the wall (STRUCTURAL MATTER PATCHING)?

    • @ArjanCodes
      @ArjanCodes  2 ปีที่แล้ว +1

      I thought it would be fun, but I’ve actually stopped with the letter board since a while.

  • @nahakuu
    @nahakuu 2 ปีที่แล้ว

    what is difference between using IF/ELIF and case?

    • @digitig
      @digitig ปีที่แล้ว

      When there are a lot of options, case is easier to read. They do the same job.

  • @airatvaliullin8420
    @airatvaliullin8420 ปีที่แล้ว

    Actually, you don't need to import typing List and use more convenient and beginner-friendly 'list[int]' syntax!

  • @rachevp
    @rachevp 2 ปีที่แล้ว

    Structural Matter Patching

  • @ingframin
    @ingframin 2 ปีที่แล้ว

    Please, tell me you can implement duff's device in Python! XD

  • @wilmhit
    @wilmhit 2 ปีที่แล้ว

    Hey do you have Odysee channel? I love these videos

  • @chriskeo392
    @chriskeo392 2 ปีที่แล้ว

    More pydantic tutorials?

  • @xpkareem
    @xpkareem 2 ปีที่แล้ว

    Surprised that there isn't a case statement in Python... why do I remember learning it? Maybe that was Javascript or something...

  • @XPXSDH
    @XPXSDH ปีที่แล้ว

    Watering your flower

  • @ThijmenCodes
    @ThijmenCodes 2 ปีที่แล้ว +1

    Hi Arjan, well explained, thank you.
    At least for me, your videos are the perfect middle between too little and too much information. Owned yourself a sub!
    Groeten uit Groningen :-)

    • @ArjanCodes
      @ArjanCodes  2 ปีที่แล้ว +1

      Glad you like them, Thijmen! Ik ben overigens geboren in Groningen (niet de stad maar provincie).

  • @CallousCoder
    @CallousCoder 2 ปีที่แล้ว

    Make It So... Engage!
    You can never not have enough Star Trek references... Live long and prosper!

    • @ArjanCodes
      @ArjanCodes  2 ปีที่แล้ว +1

      I particularly like “shut up, Wesley!” ;).

    • @CallousCoder
      @CallousCoder 2 ปีที่แล้ว

      @@ArjanCodes die arme Wil Wheaton heeft daar na 34 jaar nog trauma’s van. Iedere conventie waar hij komt roepen mensen: “Shut up Wesley!”