Structural Pattern Matching - Exciting New Python Feature (3.10)

Поділитися
Вставка
  • Опубліковано 16 лис 2024

КОМЕНТАРІ • 195

  • @alexybogomolov
    @alexybogomolov 3 роки тому +69

    I think the case(0, y) makes horizontal line with y provided, not a vertical, and vice versa.

    • @TechWithTim
      @TechWithTim  3 роки тому +13

      Good catch!

    • @xyntechx
      @xyntechx 3 роки тому +8

      I think a better fix to this bug is to say that (0, y) is a vertical-intercept (or y-intercept) and (x, 0) is a horizontal-intercept (or x-intercept). This is because for a line to be drawn, two points on said line are needed. Since we are only analysing one point at a time, it is most accurate to say that it is an intercept of either axes, provided that either x == 0 or y == 0.

    • @yamom...5918
      @yamom...5918 3 роки тому

      algebra

    • @donh9135
      @donh9135 3 роки тому

      Which axis is the x-axis? Horizontal or vertical?

    • @yamom...5918
      @yamom...5918 3 роки тому

      @@donh9135 horizontal

  • @KhannaSparsh
    @KhannaSparsh 3 роки тому +48

    Finally it would be easy and less chaotic to make command line programs.

  • @carboyau
    @carboyau 3 роки тому +8

    I was fascinated that Python avoided a switch statement and now I see this and think, "It was worth the wait". This is great and extremely flexible.

  • @dqalombardi
    @dqalombardi 3 роки тому +40

    MISTAKE IN THIS VIDEO
    You don't NEED to use an underscore. You can AND SHOULD use any valid variable name to capture the value for later use, such as if you want to include the value in a raised error.
    The reason the underscore is used is that sometimes you will not need to access that variable after capturing it and in python tools (flake8, pylint, pylance, mypy), the underscore doesn't raise a warning if the variable is never accessed as any other variable name will.

    • @jelle7937
      @jelle7937 3 роки тому +1

      As in, when none of the variables are used again in case _ , x means _ and x can be the same?

    • @dqalombardi
      @dqalombardi 3 роки тому +5

      Use an underscore if you DO NOT need to access the matched object in the case code block. Otherwise use a meaningfully-named variable to access the value inside the case code block.

    • @djlazer1
      @djlazer1 3 роки тому +7

      You only should use a variable if you need to use the value, otherwise use an underscore.

  • @lizoeshub6490
    @lizoeshub6490 3 роки тому +6

    Finally! I've been missing switch statements in Python

    • @Ollital
      @Ollital 3 роки тому

      me2. I can't wait to update the if-elif cascades to match.

  • @theweirdsquid
    @theweirdsquid 3 роки тому +2

    Congrats python for improving on an old concept in a new and innovative way! It was worth the wait for the switch statement to see this!

  • @amir78989
    @amir78989 3 роки тому +2

    as a java programmer that uses switch statements all the time. i am very excited.

  • @erikawwad7653
    @erikawwad7653 3 роки тому +1

    union and algebraic data types are cool as hell

  • @onlyforscience8255
    @onlyforscience8255 3 роки тому +5

    ohh What a nice video it is! Thanks for every information. match and switch statements are more useful than if and else. We can already use these instead of those and it prevent to repeat the same things. Now we just need to use one of them(match and switch) and It is the easier way to make code in Python in my opinion.

  • @paulmann1289
    @paulmann1289 3 роки тому +3

    The one feature I thought python lacked and now we have it. I've not been this excited about a new version of a language since VB4 came out.
    Honestly I'm hyped over the new "match" statement.

  • @xc9057
    @xc9057 3 роки тому +3

    basically an alternative to the if,elif and else statement?

  • @rice5817
    @rice5817 2 роки тому

    I have to say that I wasn't sure that I expected it to be anything hype, but damn - I will probably upgrade right away just to get this feature :D

  • @jelle7937
    @jelle7937 3 роки тому +1

    I've just started to learn JavaScript and was wondering why python did not have a switch statement... amazing video/explanation, thank you!

    • @TechWithVince
      @TechWithVince 3 роки тому

      it's not switch statement, it's pattern matching, it used a lot in functional style programming.
      Is that good for python? I don't think so, it just add another layer of complexity but the community that run the language don't care about that anymore...

    • @ericks4774
      @ericks4774 3 роки тому

      @@TechWithVince I don't see the wrong there, why is bad to add an optional feature that makes the language more completed

    • @TechWithVince
      @TechWithVince 3 роки тому

      @@ericks4774 It will train people to use recursion, because pattern matching is usually used with recursion and python is very slow at recursion.
      it's add a new way of doing the exact same thing that break python phisolophy to keep things simple.
      Otherwise I really like the feature

  • @MadPonyInteractive
    @MadPonyInteractive 3 роки тому

    I love the way you explain, thank you.
    This is really powerful stuff specially when we integrate *args and **kwargs, def gonna use the heck out of it

  • @krsaad922
    @krsaad922 3 роки тому +4

    Great job Tim!
    it's just like He Did developed Python 3.10 version

  • @gecsus
    @gecsus 3 роки тому +1

    This is "select case" like in other languages, but maybe more powerful.

  • @GrunthosThePoet
    @GrunthosThePoet 3 роки тому +3

    Wile it’s always been possible to create match statement equivalents, a real switch/case mechanism is something Python has been lacking. Considering how many other features it has, it is strange that it’s taken this long to add it to the language

    • @willful759
      @willful759 3 роки тому

      I read somewhere that this has to do with the python philosophy, it's not that they can't, it wasn't considered necesary since if elif else does roughly the same thing, however pattern matching imo is way cleaner and I'm happy to see it being added, also the whole "doesn't fit the python philosophy" I just read it somewhere, I have 0 trustable sources so I would appreciate it if someone proves/disproves me

  • @hacker64xfn99
    @hacker64xfn99 3 роки тому +4

    Nice video, can I use the "match" & "case" statements at one line like the "if" one, also I am not sure why did they add these while we can simply do almost most of it using the "if" statement and get the same result ..

  • @mohammadjawadstan9302
    @mohammadjawadstan9302 3 роки тому +1

    Thanks Tim really helpful 👍 🙏

  • @steventucker3149
    @steventucker3149 3 роки тому

    This is fantastic. I'm now very excited for 3.10. But ... as you don't put break in, I'm guessing there is no way to fall through? Perhaps with pattern matching it's not really necessary to fall through. Hmmm, need to spend some time with this. Great video!!

  • @mathewsjoby6455
    @mathewsjoby6455 3 роки тому +1

    Just a few hours ago I finished all the series and then there were none left and a few hours later there it is! Thankyou so much Tim!

  • @rorisjack1218
    @rorisjack1218 3 роки тому +1

    In the example where you used case (x, y) if x == y:
    couldn’t you have done case (x, x)?
    i think that works in pattern matchers such as ocaml’s one, but not sure about this one!
    thank tou

  • @dawidstaniek6234
    @dawidstaniek6234 3 роки тому

    Hello everybody and welcome ... Your intro is the best! And content too :)

  • @PythonPlusPlus
    @PythonPlusPlus 3 роки тому +2

    The underscore is just convention to say that you don’t care about the value of a variable.

  • @crjake
    @crjake 3 роки тому +4

    feels like Haskell

  • @humanOSx
    @humanOSx 3 роки тому +1

    Nice, feels like Elixir

  • @enriquellerena4779
    @enriquellerena4779 3 роки тому +12

    Will python 4 be a thing?

    • @GainTheReason
      @GainTheReason 3 роки тому +2

      I dont think in near future because 3.10 is valid until 2025 and 3.11 is in development already.

    • @DaniloSilva-pl3sq
      @DaniloSilva-pl3sq 3 роки тому +1

      Guido van Rossum said that if there ever will be Python 4, the transition from 3 to 4 is gonna be smoother than that of 2 to 3

    • @lakshyachopra_
      @lakshyachopra_ 3 роки тому +1

      Sure, there would be a python 4, but we would have to wait for that.

    • @onlyforscience8255
      @onlyforscience8255 3 роки тому

      @@GainTheReason No I think they will release a new version of Python but It is enough up to 2022 or 2023.

    • @RecursiveTriforce
      @RecursiveTriforce 3 роки тому +1

      I hope Python 3.15 will be Python 4 to maintain the row of natural constants found in the last releases a python version.
      Golden ratio: 1.6.1
      e: 2.7.18
      pi: 3.14.x

  • @alagappank1242
    @alagappank1242 3 роки тому +1

    Cool features , python💪💥💥...

  • @robinpage2730
    @robinpage2730 2 роки тому

    I feel like this would greatly simplify writing an interpreter or compiler and pretty much remove the need for regex. Can match statements be nested?
    Edit: yes they can!

  • @demerion
    @demerion 3 роки тому +1

    I personally wouldn't call it powerful because (at least for the examples you brought up) we do everything with if statements without increasing line count or unreadability too much :)
    There's probably a case where the if statement would take 10 lines and this one 2, but that's highly complex code anyway I guess, so it doesn't matter that much :)

  • @aleksandarharalampiev
    @aleksandarharalampiev 3 роки тому +4

    Your awesome man. Keep the good work up

  • @aledupapiecze
    @aledupapiecze 3 роки тому

    Your example_two program doesn't work properly. It would print valid coords for input like ("a", "b")

  • @carlosmspk
    @carlosmspk 3 роки тому

    Is there no way to make all cases get checked? The equivalent of turning each match from an "elif" to a "if"

  • @meysamramezani3087
    @meysamramezani3087 3 роки тому +1

    What's the difference between the match: case statement and simple if, if else??

    • @waseemq1522
      @waseemq1522 3 роки тому +1

      prolly, to be like other programming languages lmao
      but tbh it looks cleaner like this ig!

  • @cw3dv
    @cw3dv 3 роки тому +1

    I think this is the first time you are adding background music to a video...

  • @pic01
    @pic01 3 роки тому

    Yeah I like this. thanks tim

  • @xijinping3716
    @xijinping3716 3 роки тому +3

    I am still busy in building my first calculator in python (I learnt the basics around 2 months back)

    • @electricz3045
      @electricz3045 3 роки тому

      lol you can just use the math symbols on integers:
      fn = input("Enter the first number: ")
      sn = input("Enter the second number: ")
      result = fn + sn
      print(f"The Result is: {result}")

    • @cozycode8586
      @cozycode8586 3 роки тому

      ​@@electricz3045 What if he/she wants to have an input like "1+2"? Then the project becomes more complex.

    • @electricz3045
      @electricz3045 3 роки тому

      @@cozycode8586 no? You run the script and then input the numbers you want to calculate

    • @cozycode8586
      @cozycode8586 3 роки тому

      @@electricz3045 I mean rather than having two inputs and a hardcoded "+" you simply type "1+2" in a single input statement. And don't forget other operations and the order of operation. For example: "2+4*5+(4/2)". The output should be 24.

    • @cozycode8586
      @cozycode8586 3 роки тому

      @sa9097 Yeah I mean you could do that, but still. Doesn't that remove the whole meaning of the project. And what if he/she wants to type X instead of *?(You could of course just do .replace("X", "*")) I think you get my point it is a good/difficult project for beginners.

  • @GlanceOnline
    @GlanceOnline 3 роки тому

    That's sick !

  • @georgel9592
    @georgel9592 3 роки тому

    Could you please make an in depth tutorial on how to make a leaky bucket in python? I look at source code for other people’s, but I can’t understand it. I want to make a rate limiter for my API

  • @quikmath8071
    @quikmath8071 3 роки тому +2

    I’m a regular viewer of this channel. Didn’t expect to be the first to post a comment. Great tutorial s always!

  • @yimesgenmorka9478
    @yimesgenmorka9478 3 роки тому +3

    wow this is so powerful !

  • @daverei1211
    @daverei1211 3 роки тому

    Thanks Tim. Nice :-)

  • @zmitiatef5188
    @zmitiatef5188 3 роки тому

    You're the best, can u make an angular course ! 😘

  • @chikanma5021
    @chikanma5021 3 роки тому +1

    This is awesome !!

  • @octavsteaua2000
    @octavsteaua2000 3 роки тому

    What text editor are you using?

  • @ultrasacu4962
    @ultrasacu4962 3 роки тому

    What keyboard do you use now?

  • @dillonbarnes232
    @dillonbarnes232 3 роки тому

    Much easier way to create voice assistants!

  • @gameryo3691
    @gameryo3691 3 роки тому

    You will never gonna give you up!

  • @lakshayainani
    @lakshayainani 3 роки тому +2

    Much awaited feature in python !

  • @mohammadraufzahed
    @mohammadraufzahed 3 роки тому

    Awesome

  • @mohammadfoadtavakoli7118
    @mohammadfoadtavakoli7118 3 роки тому

    Hi Tim. Live the videos

  • @ibrahim47
    @ibrahim47 3 роки тому

    It's nice except its still a statement not an expression, C# got it right, Scala got it right, see how pattern matching works in them.

  • @Laszer271
    @Laszer271 3 роки тому

    Reminds me of how you code in Prolog.

  • @fackarov9412
    @fackarov9412 3 роки тому

    what about cases without "break"?

  • @mohamedchekhab276
    @mohamedchekhab276 3 роки тому

    Please make a course about python in cyber security and ethical hacking 🥀🙏

  • @enriquepenanieto9784
    @enriquepenanieto9784 3 роки тому

    Please make a flask-react project!

  • @sathianarayananalagesan6480
    @sathianarayananalagesan6480 3 роки тому

    Hey Tim put python series for socket programming
    I will waiting for that bro

  • @cartersherman925
    @cartersherman925 3 роки тому

    Jump to 2:20

  • @kulothunganug
    @kulothunganug 3 роки тому +1

    What if I have a variable named _ ?

    • @RecursiveTriforce
      @RecursiveTriforce 3 роки тому +1

      variables get overwritten...
      you could also ask: "what if i have a variable x?"

  • @mathewsjoby6455
    @mathewsjoby6455 3 роки тому +1

    Please make a video on face recognition!🧡💛❤💚💙💜🤎🥺

    • @simonrobertgreenwood
      @simonrobertgreenwood 3 роки тому +2

      check out his OpenCV tutorials the last one covers face recognition

    • @mathewsjoby6455
      @mathewsjoby6455 3 роки тому

      @@simonrobertgreenwoodThankyou so much bro! you're awesome😍. Thankyou so much for the help

    • @simonrobertgreenwood
      @simonrobertgreenwood 3 роки тому

      @@mathewsjoby6455 no problem :)

  • @Bl0xxy
    @Bl0xxy 3 роки тому

    Why not just use if and elif?

  • @felixzeller5641
    @felixzeller5641 3 роки тому

    Why dont you use pycharm anymore?

  • @trevormckellen5613
    @trevormckellen5613 3 роки тому

    recently i realized i've learned alot from Tim but wasn't subscribed. I immediatly did

    • @trevormckellen5613
      @trevormckellen5613 3 роки тому

      I came from zero to...i am pretty good right now. Coming from a third world country, the programming taught in school is just so bad(sorry). I made my school projects from what I'd learned from you. Thank you so much. Definitely told my friends about this channel

    • @Gigusx
      @Gigusx 3 роки тому

      @@trevormckellen5613 you replied to a bot... -_-

  • @GodModeMaker
    @GodModeMaker 3 роки тому

    This looks a lot useful than it sounds. It'd save me from those pesky if elifs xD

  • @qwxdw_69
    @qwxdw_69 3 роки тому

    what s the app he s editing in ?

  • @shrikantdhayje3490
    @shrikantdhayje3490 3 роки тому

    But I Thought Adding Tags In Description Is Banned In UA-cam

  • @nureinmensch3107
    @nureinmensch3107 3 роки тому

    Finally this removes all this big annoying if statements :)

  • @nicolasa.bermellferrer8025
    @nicolasa.bermellferrer8025 2 роки тому

    It feels like injecting some Haskell into python OwO

  • @kapilansaravanan3052
    @kapilansaravanan3052 3 роки тому +1

    Hey try to add some smile to that face!

  • @sebastian-yellowbunny2746
    @sebastian-yellowbunny2746 3 роки тому

    Similar to rust match statement

  • @jedymatt
    @jedymatt 3 роки тому

    Can *a be replaced by *_ ?

  • @parthkabra4968
    @parthkabra4968 3 роки тому

    It would be great if they make it fast

  • @NB-ph6cv
    @NB-ph6cv 3 роки тому +1

    or it can be written with if statement like a normal programmer :D

  • @elishashmalo3731
    @elishashmalo3731 3 роки тому +1

    Python 3.10 is the same as 3.1 though! Where’s python 4?!

    • @Altirix_
      @Altirix_ 3 роки тому +2

      since when was 10 = 1. im sure that has some serious complications to maths in general if thats true.

    • @elishashmalo3731
      @elishashmalo3731 3 роки тому +1

      @@Altirix_ decimals man, all zeros after a number that is greater than zero gets cut of, 3.1 == 3.10

    • @testk822
      @testk822 3 роки тому

      @@elishashmalo3731 But these are obviously not formatted as doubles? Version numbers such as 3.9.5 exist

    • @elishashmalo3731
      @elishashmalo3731 3 роки тому +1

      @@testk822 yeh but there has nvr been a 3.10.9 b4. The standard rule (which I believe is called “carry the 1”) still applies. This is a new precedent.

    • @Altirix_
      @Altirix_ 3 роки тому

      @@elishashmalo3731 its version numbers, not maths rounding or significant figures do not come into play. seriously did you start rounding up to python 4 when you switched to python 3.5? i sure doubt it

  • @RyanKOnk
    @RyanKOnk 3 роки тому +1

    Why not just regex?

    • @dqalombardi
      @dqalombardi 3 роки тому +3

      regex only matches strings. This can match a lot more python objects

    • @RyanKOnk
      @RyanKOnk 3 роки тому

      @@dqalombardi hmm you are right

  • @edelinealexandre
    @edelinealexandre 3 роки тому

    OCML *intensifies*

  • @wege8409
    @wege8409 3 роки тому

    Insert YandareDev reference here

  • @godwinsojan6205
    @godwinsojan6205 3 роки тому

    c++ full course please

    • @godwinsojan6205
      @godwinsojan6205 3 роки тому

      @Pínned by Tech With Tim ok I will subscribe and smash like button
      but what about cpp course
      i need cpp
      specialy embed system

    • @godwinsojan6205
      @godwinsojan6205 3 роки тому

      tim replay

    • @godwinsojan6205
      @godwinsojan6205 3 роки тому

      tim please replay by question

  • @terryr5713
    @terryr5713 3 роки тому

    Tim I am planning to get a law degree, can I combine it with computer science and cyber security!? Also I don't know any programming language

  • @gameryo3691
    @gameryo3691 3 роки тому

    We just got rickrolled

  • @SkyFly19853
    @SkyFly19853 3 роки тому

    Will it be useful for making video games in 3d?

    • @rylandavis2976
      @rylandavis2976 3 роки тому

      Rule of thumb. Don't make 3d video games in python

    • @SkyFly19853
      @SkyFly19853 3 роки тому

      @@rylandavis2976
      Oh, really? Civ 4 made in Python btw... and people still play it ( including myself ) they made CyberPink 2077 in C# and took them more than 8 years and then... they screwed everything... waste of time resources...

    • @rylandavis2976
      @rylandavis2976 3 роки тому

      @@SkyFly19853 civ 4 is not written in python. Certain parts are. Almost all of the underhood stuff is written in a more technical language because of needs to manage performance and resources. Civ 4 allows python scripting tho

    • @rylandavis2976
      @rylandavis2976 3 роки тому

      @@SkyFly19853 civ 4 was written in c++

    • @SkyFly19853
      @SkyFly19853 3 роки тому

      @@rylandavis2976
      If you check there is plenty of Python with Panda 3d.

  • @adwdwdwas
    @adwdwdwas 3 роки тому

    I'm still a beginner but this appears similar to the try and except.
    The try being cases and default(wild card) being the except 🤔

    • @jagaya3662
      @jagaya3662 3 роки тому

      I think try-except is used to catch errors, not for regular pattern-matching.
      It looks similar because it does a similar thing, but try-except for a start needs some actual errors to go into the except block - meaning you would need to write the matching in a way, that produces an actual error. The original if-elif-else chains this will replace would need conditions that produce True/False. Meanwhile Match-Case just needs patterns and it will do the comparison for you. No need for conditions, no need for forcing errors.

  • @parkermathews3422
    @parkermathews3422 3 роки тому

    Whats the point of this... Couldn't you just make a dictonary of values and get the case from that?
    Example:
    values = {"x": "This is x"}
    my_case = "x"
    value = values.get(my_case)
    print(value)

    • @jesuschris9543
      @jesuschris9543 3 роки тому

      it’s probably how you’re supposed to do it. its a lot like using an enum with switch statements in c++

    • @jagaya3662
      @jagaya3662 3 роки тому

      Dictionaries need non-mutable objects as keys and cannot match wildcards either.
      So no, it's not the same.
      It's also not the same in other languages, as the "break" keyword indicated in the beginning - because switch-case would execute all cases after the first true one. Hence the need to include "break" so it doesn't do that. This can technically be used to combine some cases.

    • @parkermathews3422
      @parkermathews3422 3 роки тому

      @@jagaya3662 ohhhh... i see

  • @elpython3471
    @elpython3471 3 роки тому +1

    3rd

  • @אנונימוס-ק5כ
    @אנונימוס-ק5כ 3 роки тому +1

    Second

  • @wesinec
    @wesinec 3 роки тому

    Aleluia

  • @joelimbergamo639
    @joelimbergamo639 3 роки тому

    I normally agree with the python org but I really don't like how this writes. But even if it's ugly it's actually really usefull

  • @vasantmayekar773
    @vasantmayekar773 3 роки тому +2

    Finally it would be easy and less chaotic to make command line programs.