1. * * * kwargs and args
This article is participating in Python Theme Month,See the event link for details
I find that most new Python programmers have a hard time figuring out the *args and *kwargs magic variables. So what are they? First of all, there is no need to write args or Kwargs. Only the (asterisk) is required. You can also write *var and **vars. Writing *args and **kwargs is just a convention. So let’s take a look at *args first.
1.1. The use of *args
*args and **kwargs are mainly used for function definitions. *args and **kwargs allow you to pass an unspecified number of arguments to a function, so when writing a function definition, you don’t need to know the number of arguments to be passed to the function. *args is used to send a list of non-keyword variable length arguments to a function. Here’s an example to help you understand clearly:
def test_args(f_arg, *argv):
print("first normal arg:", f_arg)
for arg in argv:
print("another arg through *argv:", arg)
test_args('yasoob', 'python', 'eggs', 'test')
Copy the code
First normal arg: yasoob another arg through *argv: Python another arg through *argv: eggs another arg through *argv: testCopy the code
I hope this clears up any confusion. So now let’s talk about **kwargs
1.2. Use of **kwargs
**kwargs allows you to pass arguments of variable keyword length to functions. If you want to handle named arguments in functions, you should use **kwargs. Here’s an example to keep you going with it:
def test_kwargs(**kwargs):
for key, value in kwargs.items():
print("{0} = {1}".format(key, value))
>>> test_kwargs(name="yasoob")
name = yasoob
Copy the code
So you can see how we handle the keyword argument list in our function. This is just the basics of ** Kwargs and you can see how useful it is. Now let’s discuss how to use *args and **kwargs to call functions with argument lists or dictionaries.
1.3. Call functions with *args and **kwargs
So here we’ll see how to call functions using *args and **kwargs. Think you have this little feature:
def test_args_kwargs(arg1, arg2, arg3): Print (“arg1:”, arg1) print(“arg2:”, arg2) print(“arg3:”, arg3) Now you can use *args or **kwargs to pass arguments to this small function. Here’s how:
# first with *args
>>> args = ("two", 3, 5)
>>> test_args_kwargs(*args)
arg1: two
arg2: 3
arg3: 5
# now with **kwargs:
>>> kwargs = {"arg3": 3, "arg2": "two", "arg1": 5}
>>> test_args_kwargs(**kwargs)
arg1: 5
arg2: two
arg3: 3
Copy the code
Use the * * * argsOrder of Kwargs and official Args
So if you want to use all three in a function, then the order is
some_func(fargs, *args, **kwargs)
1.4. When do you use them?
It really depends on what your requirements are. The most common use case is to make function decorators (discussed in another chapter). In addition, it can be used for monkey repair. A monkey patch means that some code is changed at run time. Suppose you have a class with a function that calls the API get_info and returns the response data. If we want to test it, we can replace the API call with some test data. Such as:
import someclass
def get_info(self, *args):
return "Test data"
someclass.get_info = get_info
Copy the code
I’m sure you can think of a few other use cases as well.