Optional Arguments in Python With *args and **kwargs

optional arguments are always placed first in the argument list This is a topic that many people are looking for. museumoftechno is a channel providing useful information about learning, life, digital marketing and online courses …. it will help you have an overview and solid multi-faceted knowledge . Today, museumoftechno would like to introduce to you Optional Arguments in Python With *args and **kwargs. Following along are instructions in the video below:

“How s it going so today. I m going to talk about the star args args and double star quarks or keyword arguments. And this is again a feature in that can be a little bit arcane if you re seeing it for the first time just. Because it looks a little bit weird.

And it has this strange sounding name right arc and quark. And what is this even good for so i want to talk about how these things work what they re good for and how you can use them to write better programs to write more pythonic python and just become a better python developer. Alright. So how do these guys work.

How do these arcs and quarks work. So let s take a look at the simple example here some defining this function foo here that takes one required parameter and then it also has these arcs and quarks here. And what it s then going to do it s it s going to print. All of these these arguments right so if you have arcs.

We re going to print them if we have quarks we have them we re going to print them and we re also going to print. The required argument and that is going to be our test bed to actually inspect what s going on behind the scenes so first of all what you can see here is that this function. Because of this definition. It will require at least one argument that s called required.

But it can also accept extra positional and keyword arguments. Now if we call this function with extra arguments. Then args will collect all of the non named or positional arguments and quarks will collect all of the keyword arguments and this happens. Because these parameters are called arcs or quarks.

But because of that little star operator right..

It s just sort of a naming convention to call them arcs and quarks and they re kind of piratey and easy to remember right arc and quark okay so we have this foo function. Now and what i want to do now is call it with various combinations of arguments. So that you can see what actually happens behind the scenes. So the first case is really easy we re just going to call foo.

Without any arguments. And in python is going to complain that hey i need at least this one argument called required. So i guess. That was kind of expected now the second case is where we would call foo with the required argument just going to put in hello.


You can see here okay we get hello as the printout. It looks like we didn t have any arcs or quarks and now what i m going to do next is i m going to call foo again. I m still giving it this string here as the required argument and then i m adding a bunch. More numbers here that are going to be the extra positional arguments.

So when i call this you can see here that it printed the the arcs here right so now we had arcs it arcs wasn t empty. But now it s actually a tuple containing all of the extra positional arguments that were collected and now we can go beyond that and besides having these extra positional arguments. I ve also added these two extra keyword arguments right called them key one and key two now when i run this you can see here that this results pretty much the same as before but now we also have this dictionary here dad contains all of the extra keyword arguments. So we didn t order the function.

Didn t really ask for any of these arguments to be passed in but it was able to handle them and the python runtime collected all of the extra arguments inside this arcs tuple and this quarks or key word args dictionary. Which means that now our function can use them for something useful so with that method you could create functions that react differently depending on how many or you know what kinds of parameters you pass in which could be useful they also allow you some other techniques that i want to show you now okay so the first technique that we can use these extra positional and extra keyword arguments for is that we can write wrapper functions that modify or you know do something to those arguments. And then actually pass them on to the original function. So here i ve redefined foo again and now foo is actually calling bar.

But before it calls bar with the same arguments..

It makes some modifications to arcs and keyword arcs. So you can see that here we re with keyword arcs is really easy because this is just a dictionary. So you can you know mutate it in place. And i just added this name key to keyword args with arcs because it s a tuple you need to create a new object and then use that because tuples are immutable so you can t add stuff to them so here.

You know what i m doing i m just taking args and i m adding this extra string here i store that as new arcs and then when i call the original function or the wrapped function. I pass all of this stuff to bar the original x and then the new args and the modified keyword arcs using this star and double star operator. And this gives us a way to wrap an existing function and actually make some modifications to the arguments that are getting passed in and then still kind of reassembling everything and calling the original function and kind of the nice thing about that is that i don t really have to know what args or keyword are x. Looks like right so i can just kind of take whatever i m given and then or i can write a function that takes whatever its given and then it can make modifications where it wants to and then pass that on now this sounds a little bit dangerous and it totally is because it can definitely create a maintenance nightmare.

READ More:  9 Best Video Glasses 2019

But in some cases when you re wrapping some external library. This could be the way to go right so i just wanted to point that out but in general. You maybe want to be careful with that all right here s another example. So this method can also be really helpful for sub classing.

So let s say you want to extend the behavior of a parent class. Without having to replicate the full signature of its constructor in the child class and for example. If you re working with an api that changes outside of your control. So you know.

This is the ever popular car class that takes a color in the mileage and then i ve extended that with the always blue car class that is going to call the parent class. So it s going to call. The parent class constructor. With this super.


Init and then it s just going to override the color and now you can see here the way this is implemented. I didn t actually duplicate the information up here the parameters up here so i didn t call this color and mileage and so on. But i just said hey give me everything that was there previously and then i took those and forwarded it to the original constructor up here and then i just went in and kind of overrode. The color to be blue.

Now. While this is a super powerful technique for wrapping these these classes are changing the behavior for class that you don t want to kind of replicate large portions of it and you maybe you just want to selectively override something if it s if it s not a class that you have control over because the third party class. Then this could be a viable method just to kind of patch that information in but on the other hand. There s some downsides associated with it as well so one downside is that now this always blue car has a really unhelpful signature in its constructor right it just says args and kwargs.

Which doesn t really tell you the kind of arguments. It expects so you would have to fix that with a good dark string. And say hey you really want to look up the car class. This is just a thin wrapper around it be careful with that now the most likely scenario.

READ More:  Replace 12v power cigarette lighter 2010 Yukon Denali XL

Where you would use a technique like that if you want to override some external class or some behavior in an external class. Which you don t control. Which is a pretty good motivation. But you know it s always dangerous territory to do that right or otherwise probably.

It s going to be you who s going to be screaming argh and quirk. If you re doing that too much so i would definitely be careful with this because it s a super potentially helpful technique about on the other hand. It could also be extremely dangerous. Okay.

There s actually a third way..

Where you can use this ticking for great effect. And that s when you re writing decorators. Which are basically wrappers around other functions. And there it s pretty much all the code examples you see out there they re going to use this technique to wrap a function and just forward arguments to it right so.

This is where you can apply that method as well so this is a really powerful feature in python and this is definitely something that i would play with and explore if you want to become a better developer and make your python code more clean and beautiful and more pythonic because it is used quite frequently so play with it and understand how it really works that would be my recommendation. But of course with a technique like that it s kind of this with a lot of power with with great power comes great responsibility right so you want to make sure you balance that to keep your code explicit enough to kind of balance. It with the need just to well you know for your convenience versus. The convenience off the future developer or your future self is going to be working with that code.

So you know again i just wanted to mention that because it s a tough choice to make it s definitely something that a great developer should be thinking about when they re writing code. So i hope this helped you out i go deeper into the subject in my book called python tricks. So if you liked this episode of python tricks. The surf.

The videos and on the youtube channel. Then be sure to check out the book. I m sure you ll love the book. It s a great way to recap all of that stuff and then go deeper into other subjects.

And it s a great way to solidify your learning and to actually have a handy reference for all of the stuff that you can apply in your day to day work all right happy python inge and i talk to you soon thanks so much ” ..

Thank you for watching all the articles on the topic Optional Arguments in Python With *args and **kwargs. All shares of s-star.org are very good. We hope you are satisfied with the article. For any questions, please leave a comment below. Hopefully you guys support our website even more.


Leave a Reply

Your email address will not be published. Required fields are marked *