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 :)
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.
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
I'm new to switch and match but Im going to start only using them to get practice
3 года назад
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.
@ 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
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 :)
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.
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.
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.
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!
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
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.
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.
*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!
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
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!
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.
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 RU-vidr 😂 So Hi Five for all our matching !!! 😂 saludos from Argentina 🥰
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?
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.
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 :-)
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.
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!
Given how slow python execution, other than compiled libraries is, is there much point optimising it? Is it just a glorified launch platform for C++ code? It is beyond my imagination why it's become the go-to for tensor processing, it makes no sense. Perhaps it's indicative of an even starker divide between people that program for the machine and people who just want predetermined things immediately
A flawed opinion certain people have had since 1999. It's 2022 and they're still around, while Python has become probably 10000x more popular in that time. Go figure.
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.
I skipped this video when it came out, because I wanted to watch it when I start using Python 3.10. But somehow RU-vid 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!
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!
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.
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 ;)
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.
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.
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().
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.
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 RU-vid 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!
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.