Structural Pattern Matching - Exciting New Python Feature (3.10)

Поделиться
HTML-код
  • Опубликовано: 6 сен 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!

  • @xc9057
    @xc9057 3 года назад +3

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

  • @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.

  • @amir78989
    @amir78989 3 года назад +2

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

  • @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

  • @erikawwad7653
    @erikawwad7653 3 года назад +1

    union and algebraic data types are cool as hell

  • @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 ..

  • @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

  • @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

  • @gecsus
    @gecsus 3 года назад +1

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

  • @krsaad922
    @krsaad922 3 года назад +4

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

  • @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.

  • @crjake
    @crjake 3 года назад +4

    feels like Haskell

  • @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 2 года назад

    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

  • @mohammadjawadstan9302
    @mohammadjawadstan9302 3 года назад +1

    Thanks Tim really helpful 👍 🙏

  • @steventucker3149
    @steventucker3149 2 года назад

    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!!

  • @PythonPlusPlus
    @PythonPlusPlus 3 года назад +2

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

  • @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

  • @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!

  • @humanOSx
    @humanOSx 3 года назад +1

    Nice, feels like Elixir

  • @dawidstaniek6234
    @dawidstaniek6234 3 года назад

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

  • @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!

  • @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!

  • @alagappank1242
    @alagappank1242 3 года назад +1

    Cool features , python💪💥💥...

  • @aleksandarharalampiev
    @aleksandarharalampiev 3 года назад +4

    Your awesome man. Keep the good work up

  • @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 :)

  • @chikanma5021
    @chikanma5021 3 года назад +1

    This is awesome !!

  • @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!

  • @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"

  • @octavsteaua2000
    @octavsteaua2000 3 года назад

    What text editor are you using?

  • @pic01
    @pic01 3 года назад

    Yeah I like this. thanks tim

  • @daverei1211
    @daverei1211 3 года назад

    Thanks Tim. Nice :-)

  • @aledupapiecze
    @aledupapiecze 3 года назад

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

  • @ultrasacu4962
    @ultrasacu4962 3 года назад

    What keyboard do you use now?

  • @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

  • @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

  • @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.

  • @lakshayainani
    @lakshayainani 3 года назад +2

    Much awaited feature in python !

  • @gameryo3691
    @gameryo3691 3 года назад

    You will never gonna give you up!

  • @GlanceOnline
    @GlanceOnline 3 года назад

    That's sick !

  • @mohammadfoadtavakoli7118
    @mohammadfoadtavakoli7118 3 года назад

    Hi Tim. Live the videos

  • @zmitiatef5188
    @zmitiatef5188 3 года назад

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

  • @fackarov9412
    @fackarov9412 3 года назад

    what about cases without "break"?

  • @cartersherman925
    @cartersherman925 3 года назад

    Jump to 2:20

  • @sebastian-yellowbunny2746
    @sebastian-yellowbunny2746 3 года назад

    Similar to rust match statement

  • @cw3dv
    @cw3dv 3 года назад +1

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

  • @mohammadraufzahed
    @mohammadraufzahed 3 года назад

    Awesome

  • @Bl0xxy
    @Bl0xxy 3 года назад

    Why not just use if and elif?

  • @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?"

  • @mohamedchekhab276
    @mohamedchekhab276 3 года назад

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

  • @yimesgenmorka9478
    @yimesgenmorka9478 3 года назад +3

    wow this is so powerful !

  • @dillonbarnes232
    @dillonbarnes232 3 года назад

    Much easier way to create voice assistants!

  • @Laszer271
    @Laszer271 3 года назад

    Reminds me of how you code in Prolog.

  • @enriquepenanieto9784
    @enriquepenanieto9784 3 года назад

    Please make a flask-react project!

  • @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.

  • @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

  • @felixzeller5641
    @felixzeller5641 3 года назад

    Why dont you use pycharm anymore?

  • @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 RUclips

  • @sathianarayananalagesan6480
    @sathianarayananalagesan6480 3 года назад

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

  • @parthkabra4968
    @parthkabra4968 3 года назад

    It would be great if they make it fast

  • @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 :)

  • @kapilansaravanan3052
    @kapilansaravanan3052 3 года назад +1

    Hey try to add some smile to that face!

  • @nicolasa.bermellferrer8025
    @nicolasa.bermellferrer8025 2 года назад

    It feels like injecting some Haskell into python OwO

  • @NB-ph6cv
    @NB-ph6cv 3 года назад +1

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

  • @GodModeMaker
    @GodModeMaker 3 года назад

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

  • @jedymatt
    @jedymatt 3 года назад

    Can *a be replaced by *_ ?

  • @nureinmensch3107
    @nureinmensch3107 3 года назад

    Finally this removes all this big annoying if statements :)

  • @edelinealexandre
    @edelinealexandre 3 года назад

    OCML *intensifies*

  • @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

  • @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 2 года назад

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

  • @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 2 года назад

      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

  • @wege8409
    @wege8409 3 года назад

    Insert YandareDev reference here

  • @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.

  • @elpython3471
    @elpython3471 3 года назад +1

    3rd

  • @user-rb1uf4ct9m
    @user-rb1uf4ct9m 3 года назад +1

    Second

  • @gameryo3691
    @gameryo3691 3 года назад

    We just got rickrolled

  • @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

  • @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.

  • @wesinec
    @wesinec 3 года назад

    Aleluia

  • @vasantmayekar773
    @vasantmayekar773 3 года назад +2

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