You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: concepts/function-arguments/about.md
+87-91Lines changed: 87 additions & 91 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,33 +10,32 @@ Parameter names should not contain spaces or punctuation.
10
10
## Positional Arguments
11
11
12
12
Positional arguments are values passed to a function in the same order as the parameters which bind to them.
13
-
Positional arguments can optionally be passed by using their parameter name.
13
+
Positional arguments can optionally be passed by using their parameter name:
14
14
15
-
Positional arguments being passed by position and by their parameter name:
16
15
17
16
```python
18
-
>>>defconcat(greeting, name):
19
-
...returnf"{greeting}{name}"
17
+
defconcat(greeting, name):
18
+
returnf"{greeting}{name}"
20
19
21
20
# Passing data to the function by position.
22
-
>>>print(concat("Hello, ", "Bob"))
23
-
Hello, Bob
21
+
print(concat("Hello, ", "Lilly"))
22
+
#-> Hello, Lilly
24
23
25
24
# Passing data to the function using the parameter name.
26
-
>>>print(concat(name="Bob", greeting="Hello, "))
25
+
print(concat(name="Glenn", greeting="Hello, "))
27
26
28
-
Hello, Bob
27
+
#-> Hello, Glenn
29
28
```
30
29
31
30
The first call to `concat` passes the arguments by position.
32
31
The second call to `concat` passes the arguments by name, allowing their positions to be changed.
33
32
34
-
Note that positional arguments cannot follow keyword arguments.
33
+
Note that positional arguments cannot follow arguments passed by name (_also called [keyword arguments][keyword-arguments]. **Not** to be confused with var-positional parameters or [**kwargs][kwargs]_).
Arguments can be positional or keyword if neither the `/` nor `*` operators are used in the parameter definitions.
129
-
Alternately, the positional-or-keyword arguments can be placed between the positional-only parameters on the left and the keyword-only parameters on the right.
130
-
131
-
Positional-only, positional-or-keyword, and keyword-only arguments:
123
+
Alternately, the positional-or-keyword arguments can be placed between the positional-only parameters on the left and the keyword-only parameters on the right:
132
124
133
125
```python
134
126
# Position-only argument followed by position-or-keyword, followed by keyword-only.
`*args` is a two-part name that represents a `tuple` with an indefinite number of separate positional arguments, also known as a [`variadic argument`][variadic argument].
160
-
`args` is the name given to the `tuple` of arguments, but it could be any other valid Python name, such as `my_args`, `arguments`, etc.
153
+
`args` is the name given to the `tuple` of arguments, but it could be any other valid Python name, such as `my_args`, `arguments`, etc.
161
154
The `*` is the operator which transforms the group of separate arguments into a [`tuple`][tuple].
162
155
163
156
~~~~exercism/note
@@ -171,70 +164,71 @@ For instance, `*` is used for multiplication, it is used for unpacking, and it i
Since a tuple can be iterated over, `args` can be passed to any other function which takes an iterable.
167
+
Since a `tuple` can be iterated over, `args` can be passed to any other function which takes an iterable.
175
168
Although `*args` is commonly juxtaposed with `**kwargs`, it doesn't have to be:
176
169
177
170
178
171
```python
179
-
>>>defadd(*args):
180
-
# args is passed to the sum function, which iterates over it.
181
-
...returnsum(args)
172
+
defadd(*args):
173
+
# args is passed to the sum function, which iterates over it.
174
+
returnsum(args)
182
175
183
-
>>>print(add(1, 2, 3))
184
-
6
176
+
print(add(1, 2, 3))
177
+
#-> 6
185
178
```
186
179
187
180
If `*args` follows one or more positional arguments, then `*args` will be what is left over after the positional arguments:
188
181
189
182
190
183
```python
191
-
>>>defadd(first, *args):
192
-
# first will be 1, leaving the values 2 and 3 in *args
193
-
...return first +sum(args)
184
+
defadd(first, *args):
185
+
# first will be 1, leaving the values 2 and 3 in *args
186
+
return first +sum(args)
194
187
195
-
>>>print(add(1, 2, 3))
196
-
6
188
+
print(add(1, 2, 3))
189
+
#-> 6
197
190
```
198
191
199
-
If one or more [default arguments][default arguments] are defined after `*args`, they are separate from the `*args` values.
192
+
If one or more [default arguments][default arguments] are defined after `*args`, they are separate from the `*args` values:
200
193
201
-
To put it all together:
202
194
203
195
```python
204
196
205
-
>>>defadd(first, *args, last=0):
206
-
...return first +sum(args) + last
197
+
defadd(first, *args, last=0):
198
+
return first +sum(args) + last
207
199
208
-
>>>print(add(1, 2, 3))
209
-
6
210
-
>>>print(add(1, 2, 3, last=4))
211
-
10
200
+
print(add(1, 2, 3))
201
+
#-> 6
202
+
203
+
print(add(1, 2, 3, last=4))
204
+
#-> 10
212
205
213
206
# This uses the unpacking operator * to separate the list elements into positional arguments.
214
207
# It does not have the same behavior as the * (packing) in *args.
215
-
>>>print(add(*[1, 2, 3]))
216
-
6
208
+
print(add(*[1, 2, 3]))
209
+
#-> 6
217
210
```
218
211
219
-
Note that when an argument is already inside an `iterable`, such as a `tuple` or `list`, it needs to be [_unpacked_][unpacking-and-multiple-assignment] before being passed to a function that takes an arbitrary number of separate arguments.
212
+
Note that when an argument is already inside an `iterable` such as a `tuple` or `list`, it needs to be [_unpacked_][unpacking-and-multiple-assignment] before being passed to a function that takes an arbitrary number of separate arguments.
220
213
This is accomplished by using `*`, which is the [unpacking operator][unpacking operator].
221
214
222
-
`*` in this context _unpacks_ the container into its separate elements which are then transformed by `*args` into a tuple.
215
+
`*` in this context _unpacks_ the container into its separate elements which are then transformed by `*args` into a `tuple`.
223
216
Where there are only positional arguments, the unpacking action must result in the same number of arguments as there are formal parameters defined.
224
217
225
218
Without unpacking the list passed into `add`, the program would error:
226
219
227
220
```python
228
-
>>>defadd(first, *args, last=0):
229
-
...return first +sum(args) + last
221
+
defadd(first, *args, last=0):
222
+
return first +sum(args) + last
230
223
231
-
>>>print(add([1, 2, 3]))
224
+
print(add([1, 2, 3]))
232
225
Traceback (most recent call last):
233
226
print(add([1, 2, 3]))
234
227
return first +sum(args) + last
235
228
TypeError: can only concatenate list (not"int") to list
236
229
```
237
230
231
+
238
232
## `**kwargs`
239
233
240
234
`**kwargs` is a two-part name that represents an indefinite number of separate [key-value pair][key-value] arguments.
@@ -246,24 +240,23 @@ Although `**kwargs` is commonly juxtaposed with `*args`, it doesn't have to be:
246
240
247
241
248
242
```python
249
-
>>>defadd(**kwargs):
250
-
...returnsum(kwargs.values())
243
+
defadd(**kwargs):
244
+
returnsum(kwargs.values())
251
245
252
-
>>>print(add(one=1, two=2, three=3))
253
-
6
246
+
print(add(one=1, two=2, three=3))
247
+
#-> 6
254
248
```
255
249
256
-
Note that the `dict.values()` method is called to iterate through the `kwargs` dictionary values.
257
-
When iterating over a dictionary, the default is to iterate through the _keys_, so `dict.values()` needs to be specified explicitly.
258
-
Following is an example of an arbitrary number of key-value pairs being passed to a function that then iterates over `kwargs.items()`:
250
+
Note that the `dict.values()` method is called to iterate through the `kwargs` dictionary `values`.
251
+
When iterating over a dictionary, the default is to iterate through the _`keys`_, so `dict.values()` needs to be specified explicitly:
259
252
260
253
```python
261
-
>>>defconcat(**kwargs):
262
-
...# Join concatenates the tuples from `kwargs.items()`
263
-
...return"".join((str(item) for item in kwargs.items()))
254
+
defconcat(**kwargs):
255
+
# Join concatenates the tuples from `kwargs.items()`
256
+
return"".join((str(item) for item in kwargs.items()))
0 commit comments