🐍 *args
Args in *args stand for arguments. It is not necessary to use args. You could name it anything. What is important is the asterisk. You could name the variable anything, but adding an asterisk in front of it, adds one awesome functionality to it. That is, it can take any number of arguments/variables from the user and save it in the form of a tuple in the variable args or whatever you might have named it.Example
def test(*args):print(args)
print(sum(args))
a = test(1,2,3,4,5)
**kwargs
Kwargs is similar to args, except it takes key-value pairs and not variables as arguments. Again, it is not necessary to name it kwargs. What is necessary is the two asterisks. It takes any number of key-value pairs and stores in a dictionary.Example
def test(**kwargs):print(kwargs)
test(a=1, b=str(2), c='three')
Comments
Post a Comment