Skip to content

Commit 37f909e

Browse files
committed
Calling functions with * and **
1 parent 763c461 commit 37f909e

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

05 Functions/2 Functions with arbitrary arguments.md

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ nav_order: 2
66

77
# Functions with arbitrary arguments
88

9+
## Defining functions with arbitrary arguments
10+
911
In Python, it is possible to create functions with any number of arguments, not specified in advance. For this purpose, when defining the function, we give a special argument `*args` (an asterisk `*` at the beginning is the important part: the name can be anything, but it is customary to use _args_ and I recommend sticking to this convention):
1012

1113
```python
@@ -23,7 +25,7 @@ Similarly, a function can accept any number of arguments given by name. For this
2325
def function2(**kwargs):
2426
print(kwargs)
2527
```
26-
When calling this function, you can pass any number of arguments with any name . Inside the function, the argument `kwargs` will be a **dictionary** containing all the arguments given. For example:
28+
When calling this function, you can pass any number of arguments with any name. Inside the function, the argument `kwargs` will be a **dictionary** containing all the arguments given. For example:
2729

2830
```python
2931
function2(a=1, b=2, c=3) # {'a': 1, 'b': 2, 'c': 3}
@@ -79,6 +81,40 @@ def print(*args, sep='', end='\n', file=sys.stdout, flush=False):
7981
"""
8082
```
8183

84+
## Calling functions with any arguments
85+
86+
Similarly like in function definition, you may use `*` and `**` symbols when calling them. See the following example:
87+
```python
88+
def function(a, b, c):
89+
print(a, b, c)
90+
91+
data = [1, 2, 3]
92+
function(*data)
93+
```
94+
This is equivalent to calling this function as
95+
```python
96+
function(1, 2, 3)
97+
```
98+
99+
In general, when calling a function as `function(*sequence)`, the sequence is expanded and its elements are passed as positional parameters. Similarly, `function(**dictionary)` is equivalent to passing the dictionary elements as named parameters. So the function from the example above can be called as
100+
```python
101+
params = {'a': 1, 'b': 2, 'c': 3}
102+
function(**params)
103+
```
104+
The keys of the dictionary passes to a function with `**` **must be strings**. In other case, `TypeError` will be raised.
105+
106+
The above method of calling function can be used regardless of its definition: it will work for both classical functions and the functions with arbitrary arguments described above.
107+
108+
A typical use case is printing all elements of a sequence:
109+
```python
110+
data = [1, 2, 3, 4]
111+
print(*data, sep=', ')
112+
```
113+
or even
114+
```python
115+
print(*"abcd", sep='-')
116+
```
117+
The last example will print “`a-b-c-d`”.
82118

83119
<hr/>
84120

0 commit comments

Comments
 (0)