@@ -114,16 +114,16 @@ now kwargs is {'b': 2, 'a': 1}
114
114
Traceback (most recent call last):
115
115
File " <stdin>" , line 1 , in < module>
116
116
TypeError : thing() takes 0 positional arguments but 2 were given
117
- >> > def print_box (message , border = ' * ' ):
117
+ >> > def print_box (message , border ):
118
118
... print (border * len (message))
119
119
... print (message)
120
120
... print (border * len (message))
121
121
...
122
- >> > kwargs = {' message' : " Hello World!" , ' border' : ' - ' }
122
+ >> > kwargs = {' message' : " Hello World!" , ' border' : ' * ' }
123
123
>> > print_box(** kwargs)
124
- ------------
124
+ ************
125
125
Hello World!
126
- ------------
126
+ ************
127
127
>> >
128
128
```
129
129
@@ -209,24 +209,30 @@ def move(source, destination, *, overwrite=False, backup=False):
209
209
```
210
210
211
211
The ` * ` between ` destination ` and ` overwrite ` means that ` overwrite ` and
212
- ` backup ` must be given as keyword arguments.
212
+ ` backup ` must be given as keyword arguments. The basic idea is really
213
+ simple: now it's impossible to overwrite by doing `move('file1.txt',
214
+ 'file2.txt', True)` and the overwrite must be always given like
215
+ ` overwrite=True ` .
213
216
214
217
``` py
215
- >> > move(' file1.txt' , ' file2.txt' , overwrite = True )
216
- deleting file2.txt
218
+ >> > move(' file1.txt' , ' file2.txt' )
217
219
moving file1.txt to file2.txt
218
220
>> > move(' file1.txt' , ' file2.txt' , True )
219
221
Traceback (most recent call last):
220
222
File " <stdin>" , line 1 , in < module>
221
223
TypeError : move() takes 2 positional arguments but 3 were given
224
+ >> > move(' file1.txt' , ' file2.txt' , ' file3.txt' )
225
+ Traceback (most recent call last):
226
+ File " <stdin>" , line 1 , in < module>
227
+ TypeError : move() takes 2 positional arguments but 3 were given
228
+ >> > move(' file1.txt' , ' file2.txt' , overwrite = ' file3.txt' )
229
+ deleting file2.txt
230
+ moving file1.txt to file2.txt
222
231
>> >
223
232
```
224
233
225
- As you can see, our new move function also forces everyone to use it
226
- like ` move('file1.txt', 'file2.txt', overwrite=True) ` instead of
227
- ` move('file1.txt', 'file2.txt', True) ` . That's good because it's hard to
228
- guess what a positional ` True ` does, but it's easy to guess what
229
- ` overwrite=True ` does.
234
+ Doing ` overwrite='file3.txt' ` doesn't make much sense and it's easy to
235
+ notice that something's wrong.
230
236
231
237
## When should we use these things?
232
238
@@ -246,8 +252,7 @@ I don't recommend using keyword-only arguments with functions like our
246
252
` print_box ` . It's easy enough to guess what ` print_box('hello', '-') `
247
253
does, and there's no need to deny that. It's much harder to guess what
248
254
` move('file1.txt', 'file2.txt', True, False) ` does, so using
249
- keyword-only arguments makes sense and also solves the file deleting
250
- problem.
255
+ keyword-only arguments makes sense.
251
256
252
257
## Summary
253
258
@@ -258,8 +263,8 @@ problem.
258
263
dictionaries and keyword arguments.
259
264
- Adding a ` * ` in a function definition makes all arguments after it
260
265
keyword-only. This is useful when using positional arguments would
261
- look implicit, like the True and False in `move('file1.txt',
262
- 'file2.txt', True, False)`.
266
+ look implicit, like the True and False in
267
+ ` move('file1.txt', 'file2.txt', True, False)` .
263
268
264
269
***
265
270
0 commit comments