📝 Zusammenfassung
openai-gpt-4o-mini
## HAUPTTHEMA
In diesem Video werden einige oft unbekannte, aber interessante Features von Python vorgestellt, die die Effizienz beim Programmieren verbessern können.
## KERNPUNKTE
• **Match Statement**: Eine neue Funktion in Python 3.10, die ähnlich wie ein Switch-Statement in anderen Programmiersprachen funktioniert und die das Schreiben von verschachtelten If-Anweisungen vereinfacht.
• **Mustererkennung**: Neben einfachen Vergleichen können auch komplexe Datenstrukturen wie Listen und Dictionaries mithilfe des Match-Statements strukturell abgeglichen werden.
• **Datenklassen**: Eine Funktion, die die Erstellung von Klassendefinitionen vereinfacht, indem sie automatisiert Methoden wie `__init__`, `__eq__` und `__repr__` implementiert, wodurch Boilerplate-Code reduziert wird.
• **Positionale und Keyword-Parameter**: Neue Möglichkeiten, Parameter in Funktionen zu definieren, um zu erzwingen, dass bestimmte Argumente nur positionell oder nur per Keyword übergeben werden, was die Robustheit und die Rückwärtskompatibilität des Codes erhöht.
• **Bedeutung für APIs**: Der Einsatz dieser Features hilft beim Entwickeln stabiler APIs, da die Namen von Parametern ohne Bruch bestehender Nutzerimplementierungen geändert werden können.
## FAZIT/POSITION
Das Video hebt hervor, dass viele moderne Python-Features selten genutzt werden, obwohl sie den Entwicklungsprozess erheblich erleichtern können. Es wird empfohlen, diese Funktionen zu nutzen, um den eigenen Code effizienter und übersichtlicher zu gestalten.
Python is definitely an interesting language
that has many unique features. However, many of these features are never actually used out in the wild
because people simply don't know they exist. So in this video, I'm going to go over some more
modern and relatively new Python features that I rarely see used that I think are interesting
and that you should definitely know about. They're not all in the most recent version of Python,
but they're things that are in modern Python that I think are worth knowing. But that's it. Let's dive in. Now, the first feature I have on my list
here is called the match Statement. Now it's also referred to as structural pattern
matching, has a few different names. And it was released in Python version 3.10. Now, you've likely seen this before, but I doubt
that you've actually used it in your code. And when I review Python code,
many people don't actually seem to use this. Okay, so what is the match statement? Well, the match statement
when you simply works exactly like a switch statement
in various other programing languages. For example, let's just move this down here. You can have a look at this code right here. You see we have some inner function. This function has a match statement inside of it. And all we're doing is we're trying to match the
status to one of these various cases that we have. So we have a case for success. That simply means if status is equal to success,
go ahead and do this. We have a case for error means same thing. If it's error
you know go ahead and do this pending do this. And then we have a default case a default case
is kind of like an L statement or default. If you're familiar with a switch statement
where if the status doesn't match any of these, will go ahead into this default case. default case isn't required.
We don't have to have it. But in this case we do, where
we just put the default case to handle anything else. Okay. So if we were to run this function here, you can see
that we can kind of go through this match statement and we can process all of these different values
that we have in our list. And then we will get the output, you know, operation completed, an error occurred still in progress, etc.,
so that alone is pretty useful. It can allow us to prevent writing,
you know, a ton of nested if statements. And we can just write it like this match statement
where we're trying to match a particular value. However, where this gets more interesting is when we
get into a more complex pattern matching scenario. So in this match statement
doesn't just work to match strings or numbers or, you know, particular value,
you can actually match patterns. So have a look at this second example down here. we're able to do
is we have a case for simply the value zero right. So just like before we can match if data is equal
to zero we also can use this pipe operator. And we can check for multiple values. So we can check
if it's one or if it's two or if it's three. And all of that will be handled by this case. Then we can also check for the structure,
which is why a lot of times people refer to this as structural pattern
matching of the object, which is data. So in this case we can say case first and second. Now what this is saying
is we're looking for any kind of data where we have an array or sorry, a list
I should say in Python of two values. So for example, if you look down here we have apple and banana
that would match with this pattern right here. And then we would be able to extract the values. So apple and banana were the first and second item
by using the variables first and second. know it looks a little bit weird. It looks kind of like magic,
but that's how this works. Now same thing with a dictionary or an object in
Python, whatever you want to refer to it as, right. What we're able to do is we have name. So the key name, some value
name the key age some value h. In this case we're matching with anything that is
a dictionary that contains exactly these two keys. And then we can strip out what the values are
and print them or process them however we want. Now we also can just check if something is a string. This is kind of interesting right?
We could just use the string function. And now we're saying okay
I want to match anything that is a string. Then if we go down here this is the default case. I'm going to run the code for you.
So you can quickly see that this does indeed work. And notice that we get all of the match statements
here up above. Right. And then same thing.
We have all of the match statements here. Now what's important to note
you use the match statement that it's going to attempt to match in the order
in which you write these cases. So for example, if we change this to zero, right, and we had some string zero, it would actually match
with this first statement here. Not with this statement down here okay. Because it's going to go into the first case
that it sees, especially because we have the return and there's a lot of other information.
Sorry about the match statement. You could do some really interesting complex stuff
here I definitely would suggest checking it out. leave a link to the documentation
in the description down below. This is a feature that I rarely see used,
which can be extremely powerful, especially when you have a variety
of different type of data and you want to process that
in some kind of format, right? So we can look for the exact type of pattern
really simplifies and cleans up our code. And then we can handle all of these different cases
as we see fit. Now the next feature on my list
is actually relatively old compared to the other ones on this list. However, it is still something
that's definitely worth knowing about and that I see few Python developers,
especially beginners or intermediates using. Now that's called data classes,
and in order for us to see the value of this, we need to actually look at how you would
typically write a class before data classes exist. So in Python,
if you want to have a class that represents data, so represents like a user or a book
or some kind of entity, you would typically write it
something like this where you have this init method. Maybe you take in an ID, some name,
maybe some roles for this particular user, and then you're almost
always going to be defining a few methods on these objects
to make them more usable in your code. For example, you use this magic method wrapper
which gives you a string representation of the object
so you can actually see what it looks like. If you're debugging the code, for example, and you write something like this, we have user name
and then the roles. Then you have something like equals for example, where you're going to check
the equivalence of two objects. You might have the string method,
you might have a few other ones as well. Right. But a lot of times
when you're just trying to represent data, you're almost always
just writing the same kind of boilerplate code. You're always writing the same equal method,
you're always writing the same wrapper method. And there isn't really good reason to do that,
other than the fact that there's no better way. So what I'm going to show you now is what's called the data class,
which avoids you having to do all of this. So I'm going to open up this example here. And notice that we have this class right here,
which is a data class. And this is exactly the same as the class
that you just saw. It's equivalently pretty much the exact same. There's a few very, very minor differences. And it's written in significantly less code. There was no init method. There's no equal method. There's no wrapper method. Right. We didn't have to do any of that. But this actually functions
the exact same way on this data class. I can use the double equal sign
right between two user objects, and it will test for equivalence in the exact
same way as our other class. I can try to print it note
and when I print it out I'm going to get that wrapper where it's going to show me exactly
what this looks like in that debugging format. And by default, if I don't pass a roll, it's
automatically going to be an empty list. Because of this field
that I brought in here from the data classes Now I have a whole video on data classes which I will link on screen, which you can watch
if you want the really in-depth explanation. But the point is these are very, very useful. Now one thing to note here
is that I did enable frozen on this because I decorated it with the data class decorator,
and when I do that, it means that you cannot change the values
inside of this user object. You can freeze it, right? You can make it immutable, which is also
an interesting component of data classes. So in order to make something a data class,
you use the data class decorator. You define the different fields that you want. You can use standard Python types
or you can use from the typing module. For example, if you want something like a list. And then if you want some kind of default value,
you can do that with this field. So default factory means create a new list. It's important
you do like this and not define a list. Due to how Python
kind of interprets that list object. And then if we come down here another example,
we have a product for example. Right. We have a price we have in stock ID name. In this case it's mutable. And if we look down here, these are the operations
you can perform on these data classes that are automatically implemented
that you don't need to write yourself. So I can create an instance of user. And I didn't need to write a. Net. I can have the ID,
I can have the name, I can have the rules. I don't need to pass the rules. Same thing with the product. You know I can pass the values. I didn't need to write the init method.
Then I can print these out. When I do
that it's automatically going to use a wrapper method for me,
which I'm going to show you in one second. And then you can check for the equivalence
of these values. Right. And an equal method is automatically implemented
for you. Same thing with the immutability
which will be enforced which I'll show you. And then this works really nicely
with the match statement. So what I can actually do is
I can now use these objects with the match statement, and I can check
if a product has the price of zero. Right. I can check if the product has a price
that's greater than 1000 or something, right? Where I can check if it's a regular product. So it's kind of a cool thing you can do with match. If I run this code here, scroll down and notice
that we get the user right. So this is automatically implemented for us. We get product automatically implemented. We get the equal method automatically implemented. Same thing here. You know user cannot modify the field name
because immutable. And then it's a fine. So one of our products was expensive
because we made it $1,200 okay. So the data class is super super useful. Again I'll put that video on screen
and just consider right this versus this. Which one would you rather write? Of course you would rather use the data class. Now the next feature that I have for you is something
called the positional or keyword only parameters. Now, parameters in Python are notoriously
a little bit confusing because of all of the different combinations of ways
that you can call them. So I want to quickly go into a bit of a primer about parameters
and then show you this relatively new feature. All right. So let's have a look at this function
here. Right. We have some function. We have some values now a b c d right. These are our parameters. Now when I call the function I can call it using
what's known as positional parameters. So I can say you know my function. And I can pass 123, four or something. Right. And when I do that I'm now assigning the value
a to one, b to two, c to three, d to four. So I can do that. That's totally fine. But I also could do something like b equals
to a equals one okay. And then I can say c equals three d equals whatever. And I can pass these in like kind of a random order. Whatever order that I want. And I can kind of pick and choose
if I want to pass them positionally or if I want to pass them using a keyword argument,
which is what I'm doing right here. And there's all kinds of other combinations of ways
that I could call this function. And it can be a little bit confusing. Now it's fine, right. That's just by default how functions are written. But in Python there's actually a way to enforce
the way in which your functions are called. And that is by using this fancy operator right here,
which is the slash. Now this slash forces arguments to be passed
positionally only. I know it seems a bit weird. Why would you do this? We'll talk about that
a little bit later. But by me implementing this slash
now inside of these function parameters, which is something you may see in larger
libraries, it now doesn't allow me to pass name using a keyword argument. And again
we'll talk about why that's important in a second. So you can look here
and you can see I can call it in this way. Right. I can call it with Alice, I can call with Bob
and high, I can call it with, you know, Charlie. And the greeting is equal to hey,
because name is passed positionally where I'm not manually defining,
you know, name is equal like this to Alice. However, if I go down here and I try to call this, I say, you know, greet name is equal to David,
you'll see that we actually get a type error. And it will tell us that this is a positional
only argument or parameter, and that I can not call it by specifying the name. So let me just call this function
and show you what that looks like. Okay. And you can see all three of these function
calls are worked properly. And then when I did try to call it here
you can see the result. For it
said this was a positional only argument. Right. So it got some positional on the arguments passed. There's a keyword argument name. So you are able to do this enforcement okay. Now let me quickly talk about why
that's actually important. The reason why this is interesting
actually applies more to APIs. And if you're creating libraries
that other people are going to be using, and that's because this allows you to make significantly
more robust functions and just APIs in general, so that you can control the way in which they're used
and make sure they stay backwards compatible. So, for example, if callers can't use a name, right,
if I'm not able to use, you know, name is equal to whatever and pass the parameter or, you know,
values equal to whatever or max is equal to two. If I can't use that right as a keyword argument, then it means that I can rename
this parameter later on. And I'm not going to break any user's code. So this is especially important
when you're writing something that you know is going to change in the future. If you make sure that something is only passed
positionally, then the name of the positional argument doesn't
actually matter, and you can change it to anything you want in the future or implement a new,
for example, keyword argument that has the same name. So it's just allows flexibility to you
as someone who's writing these functions. Now, again, imagine
the code you're writing is used by someone else, and maybe millions of people are using it. And then all of a sudden
you change the name of one of your parameters. If it could have been called by keyword
and you now change it, you're going to break a bunch of people's
code, right? So that's kind of one of the main reasons. Now another one is semantics, right. So some of the parameters
don't have meaningful names. For example x y ab you know, one two or whatever. Right. So when you force the use of positional arguments,
you signal to the user that this is more used internally in the function and to pass it
positionally rather than trying to name the keywords. Now same thing. As I said before,
Rim for future keyword updates, right? So you can keep the names available
to use them later on. And it's also more consistent with built in functions
where a lot of the built in functions, work like this already,
where you can only pass values positionally. Right? You can't pass them with keyword arguments. So hopefully that makes a little bit of sense. Now to kind of continue this,
you also can force arguments to be only keyword. So not be pass positional and be pass keyword only. And that's by using this asterix. So the way this works is anything that I want to be
positional only I put before this forward slash. If I do that anything before this. So you know pause only to write. All of these can only be passed by position. Then anything after this can be passed
freely as normally, so it can be passed by position or by keyword. Okay, so regular I can pass this
with a keyword argument or I can pass normally. Then if I put an asterix, anything to the right of
this can only be passed by a keyword. So now I cannot pass this value positionally,
I can only pass it by a keyword, and similar reasons for doing that apply
to as why you would only pass a positionally. I know this is kind of advanced,
you know, niche Python code, but that's the point of this video is to show you guys
some new stuff that you probably never seen before. And I know I didn't see this until I started
looking deeper into Let's actually run the code here. And you can see now that this works, right. It says, you know, makes parameters positional
only one regular two keyword only three. I just quickly change this example back so
it would work with the way that it was being called. Anyways,
that is pretty much going to wrap this up. Now. If you've made it to this point in the video,
then I can tell you definitely value learning you like more of those advanced topics. And I mean, you're spending your time
watching a video like this. And if that's the case, then I think you would
definitely benefit from the sponsor of today's video, which is brilliant. Brilliant is where you learn by doing with thousands of interactive lessons in math, data
analysis, programing and AI. They adopt a first principles approach,
ensuring you understand the why behind each concept. Every lesson is interactive,
engaging you in hands on problem solving, which is proven to be six times more effective
than simply watching lectures. The content is developed by top notch educators. Research, and professionals from renowned
institutions like MIT, Caltech, and Google. Brilliant emphasizes
enhancing your critical thinking abilities through active problem solving rather than memorization. As you learn specific subjects, you're simultaneously
training your mind to think more Consistent daily learning is crucial, and brilliant makes it effortless
with their bite sized lessons allowing you to acquire meaningful knowledge in just a few minutes each day,
which is perfect for replacing idle screen time. Additionally, brilliant offers a comprehensive range
of computer science and Python courses, as well as extensive
AI workshops guiding you from a complete beginner to an expert through practical, hands on lessons
to learn. For free on Brilliant's,
go to brilliant.org Agtech with Tim, scan the QR code on screen or click
the link in the description. Brilliant is also giving our viewers 20% off
an annual premium subscription, which gives you unlimited daily access
to everything on brilliant. Thanks to brilliant for sponsoring this video
and I look forward to seeing you in the next one.