-
Notifications
You must be signed in to change notification settings - Fork 1
/
35_mutable_default_value.py
123 lines (84 loc) · 3.3 KB
/
35_mutable_default_value.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
def send_many_sms(sender, message, recipients=["447700900770"]):
for recipient in recipients:
print(f"Sending a message to {recipient}")
print("Done.")
# Previous calls still work ---------------------------------------------------
# Call with positional arguments:
send_many_sms("447700900730", "How's it going?")
# -> Sending a message to 447700900770
# Call with named argument:
send_many_sms("447700900730", "How's it going?", recipients=["447700900770", "447700900771"])
# -> Sending a message to 447700900770
# -> Sending a message to 447700900771
# Providing value for sender: -------------------------------------------------
# Positional args:
send_many_sms("447700900730", "How's it going?", ["447700900770", "447700900771"])
# Optional arg as named argument:
send_many_sms("447700900730", "How's it going?", recipients=["447700900770", "447700900771"])
print("Now let's 'log' the SMS messages")
def send_many_sms(sender, message, recipients=["447700900770"]):
# Always send a copy to our "logging" phone:
recipients.append("447700900666")
for recipient in recipients:
print(f"Sending a message to {recipient}")
print("Done.")
# Call with positional arguments:
send_many_sms("447700900730", "How's it going?")
# Sending a message to 447700900770
# Sending a message to 447700900666
# Done.
send_many_sms("447700900730", "How's it going?")
# Sending a message to 447700900770
# Sending a message to 447700900666
# Sending a message to 447700900666
# Done.
send_many_sms("447700900730", "How's it going?")
# Sending a message to 447700900770
# Sending a message to 447700900666
# Sending a message to 447700900666
# Sending a message to 447700900666
# Done.
print("Provide recipients value:")
recipients = ["447700900770"]
send_many_sms("447700900730", "How's it going?", recipients)
print(recipients)
# ['447700900770', '447700900666']
# -----------------------------------------------------------------------------
print()
print("Let's fix that")
def send_many_sms(sender, message, recipients=None):
if recipients is None:
recipients = ["447700900770"]
# Always send a copy to our "logging" phone:
recipients.append("447700900666")
for recipient in recipients:
print(f"Sending a message to {recipient}")
print("Done.")
send_many_sms("447700900730", "How's it going?")
# Sending a message to 447700900770
# Sending a message to 447700900666
# Done.
send_many_sms("447700900730", "How's it going?")
# Sending a message to 447700900770
# Sending a message to 447700900666
# Done.
# Warning! We are STILL modifying provided recipients!
recipients = ["447700900770"]
send_many_sms("447700900730", "How's it going?", recipients)
print(recipients)
# ['447700900770', '447700900666']
# -----------------------------------------------------------------------------
print()
print("Let's fix the 'modify parameter problem:")
def send_many_sms(sender, message, recipients=None):
if recipients is None:
recipients = ["447700900770"]
# Create a *new* `recipients` variable:
recipients = recipients + ["447700900666"]
for recipient in recipients:
print(f"Sending a message to {recipient}")
print("Done.")
recipients = ["447700900770"]
send_many_sms("447700900730", "How's it going?", recipients)
print(recipients)
# ['447700900770']