@@ -114,16 +114,16 @@ now kwargs is {'b': 2, 'a': 1}
114114Traceback (most recent call last):
115115 File " <stdin>" , line 1 , in < module>
116116TypeError : thing() takes 0 positional arguments but 2 were given
117- >> > def print_box (message , border = ' * ' ):
117+ >> > def print_box (message , border ):
118118... print (border * len (message))
119119... print (message)
120120... print (border * len (message))
121121...
122- >> > kwargs = {' message' : " Hello World!" , ' border' : ' - ' }
122+ >> > kwargs = {' message' : " Hello World!" , ' border' : ' * ' }
123123>> > print_box(** kwargs)
124- ------------
124+ ************
125125Hello World!
126- ------------
126+ ************
127127>> >
128128```
129129
@@ -209,24 +209,30 @@ def move(source, destination, *, overwrite=False, backup=False):
209209```
210210
211211The ` * ` 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 ` .
213216
214217``` py
215- >> > move(' file1.txt' , ' file2.txt' , overwrite = True )
216- deleting file2.txt
218+ >> > move(' file1.txt' , ' file2.txt' )
217219moving file1.txt to file2.txt
218220>> > move(' file1.txt' , ' file2.txt' , True )
219221Traceback (most recent call last):
220222 File " <stdin>" , line 1 , in < module>
221223TypeError : 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
222231>> >
223232```
224233
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.
230236
231237## When should we use these things?
232238
@@ -246,8 +252,7 @@ I don't recommend using keyword-only arguments with functions like our
246252` print_box ` . It's easy enough to guess what ` print_box('hello', '-') `
247253does, and there's no need to deny that. It's much harder to guess what
248254` 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.
251256
252257## Summary
253258
@@ -258,8 +263,8 @@ problem.
258263 dictionaries and keyword arguments.
259264- Adding a ` * ` in a function definition makes all arguments after it
260265 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)` .
263268
264269***
265270
0 commit comments