Variable-Length Arguments in Python Functions

Variable-Length Arguments in Python Functions

You may need to have a function for more arguments than you specified while defining the function. These arguments are called variable-length arguments and are not named in the function definition, unlike required and default arguments.

The syntax for such a function is this:

def func_name([formal_args,] *var_tuple ):

func_body

We can see an asterisk (*) before the variable name. It will hold the values of all nonkeyword variable arguments. You should remember that this tuple remains empty if no additional arguments are specified during the function call. The tuple will be used if necessary. Let’s see a real example:

def gostek(a, *krotka):

print(a)
for kon in krotka:

print(kon)

Now we can see how it works. We are going to call the function gostek() with variable number of arguments:

gostek(100)
gostek(100,200)
gostek(100,200,”Arnold”)
gostek(100,200,”Arnold”, 34.5)

We can see the result:

>>> gostek(100)
100
>>> gostek(100,200)
100
200
>>> gostek(100,200,”Arnold”)
100
200
Arnold
>>> gostek(100,200,”Arnold”,34.5)
100
200
Arnold
34.5
>>>

Named arguments can’t be placed at the end of an argument list, so not-named arguments must be at the beginning of the argument list. Provided arguments are being assigned to parameters in order:

— not-named arguments in sequence

— named arguments by name

— the rest of arguments is sent to the variable with asterisk

Do not assign two arguments to one parameter! For example, one not-named and the other named.

We can also provide variable arguments to our function as a dictionary. In this case, our variable is preceded by two asterisks. Here, we can use named arguments, which names don’t correspond to any parameters of our function. These arguments are inserted into the dictionary that is a parameter going to the function. Argument names are keys, and their values are assigned to these keys. It’s simple, so let’s look at the example:

def gostek(*krotka, **kwkrotka):

for kon in krotka:

print(kon)

for kon in kwkrotka:

print(kon)

We can see the result:

>>> gostek(2,3,5,6)
2
3
5
6
>>> gostek(2,3,5,6,c=8,d=9)
2
3
5
6
d
c
>>> gostek(2,3,5,6,c=8,d=9,e=”Lolek”,f=”Karolek”)
2
3
5
6
d
e
f
c
>>>

In the case of **kwkrotka, we have only keys, without values. To get keys and values, we can do that:

def gostek(*krotka, **kwkrotka):

print(krotka)

print(kwkrotka)

We can see the result:

>>> gostek(2,3,5,6,c=8,d=9,e=”Lolek”,f=”Karolek”)
(2, 3, 5, 6)
{‘d’: 9, ‘e’: ‘Lolek’, ‘f’: ‘Karolek’, ‘c’: 8}
>>>

I hope everything is clear. If you have any questions or suggestions, please write to me.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s