forked from dabeaz/python-cookbook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
42 lines (34 loc) · 667 Bytes
/
Copy pathexample.py
File metadata and controls
42 lines (34 loc) · 667 Bytes
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
# Examples of a function with default arguments
# (a) Dangers of using a mutable default argument
def spam(b=[]):
return b
a = spam()
print(a)
a.append(1)
a.append(2)
b = spam()
print(b) # Carefully observe result
print('-'*10)
# (b) Better alternative for mutable defaults
def spam(b=None):
if b is None:
b = []
return b
a = spam()
print(a)
a.append(1)
a.append(2)
b = spam()
print(b)
print('-'*10)
# (c) Example of testing if an argument was supplied or not
_no_value = object()
def spam(b=_no_value):
if b is _no_value:
print("No b value supplied")
else:
print("b=", b)
spam()
spam(None)
spam(0)
spam([])