@@ -44,8 +44,34 @@ username, password = login()
44
44
That gets kind of messy if there are more than three values to return,
45
45
but I have never needed to return more than three values. If you think
46
46
you need to return four or more values you probably want to use [ a
47
- class] ( ../basics/classes.md ) instead and store the values like
48
- ` self.thing = stuff ` .
47
+ class] ( ../basics/classes.md ) instead.
48
+
49
+ For example, instead of this...
50
+
51
+ ``` python
52
+ def get_new_info (username ):
53
+ print (" Changing user information of %s ." % username)
54
+ username = input (" New username: " )
55
+ password = input (" New password: " )
56
+ fullname = input (" Full name: " )
57
+ phonenumber = input (" Phone number: " )
58
+ return username, password, fullname, phonenumber
59
+ ```
60
+
61
+ ...you could do this:
62
+
63
+ ``` python
64
+ class User :
65
+ # you probably want to make many other user related things too, add
66
+ # them here
67
+
68
+ def change_info (self ):
69
+ print (" Changing user information of %s ." % self .username)
70
+ self .username = input (" New username: " )
71
+ self .password = input (" New password: " )
72
+ self .fullname = input (" Full name: " )
73
+ self .phonenumber = input (" Phone number: " )
74
+ ```
49
75
50
76
## \* args
51
77
@@ -240,19 +266,15 @@ There's nothing wrong with returning a tuple from a function, and you
240
266
are free to do that whenever you need it.
241
267
242
268
We don't need ` *args ` and ` **kwargs ` for most of the functions we write.
243
- Often functions just do something and arguments are a way to change how
244
- they do that, and by not taking ` *args ` or ` **kwargs ` we can make sure
245
- that we'll get an error if the function gets an invalid argument.
246
-
247
269
When we need to make something that takes whatever arguments it's given
248
270
or call a function with arguments that come from a list we need ` *args `
249
271
and ` **kwargs ` , and there's no need to avoid them.
250
272
251
273
I don't recommend using keyword-only arguments with functions like our
252
- ` print_box ` . It's easy enough to guess what ` print_box('hello', '-') `
253
- does, and there's no need to deny that. It's much harder to guess what
254
- ` move('file1.txt', 'file2.txt', True, False) ` does, so using
255
- keyword-only arguments makes sense.
274
+ ` print_box ` . It's easy enough to guess or remember what
275
+ ` print_box('hello', '-') ` does, and there's no need to deny that. It's
276
+ much harder to remember what ` move('file1.txt', 'file2.txt', True, False) `
277
+ does, so using keyword-only arguments makes sense.
256
278
257
279
## Summary
258
280
0 commit comments