-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathkeyword_demo.py
More file actions
88 lines (50 loc) · 986 Bytes
/
keyword_demo.py
File metadata and controls
88 lines (50 loc) · 986 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
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
# -*- coding: utf-8 -*-
# <nbformat>3.0</nbformat>
# <markdowncell>
# ## keyword argument scope demo
# <markdowncell>
# Define a variable in the global scope:
# <codecell>
a = 5
# <markdowncell>
# Use that variable in a function:
# <codecell>
def add(x):
return x + a
# <markdowncell>
# call the function -- a is used:
# <codecell>
add(3)
# <markdowncell>
# change a
# <codecell>
a = 12
# <codecell>
add(3)
# <markdowncell>
# The new a is used
# <markdowncell>
# But what if I don't want the results to depend on what a gets re-set to.
#
# But I don't want a constant, either...
#
# Set a keyword argument:
# <codecell>
def add(x, a=a):
return x + a
# try it:
add(3)
# <markdowncell>
# it used the last value.
#
# reset a
# <codecell>
a = 100
add(3)
# <markdowncell>
# Still used the original value!
# <markdowncell>
# ## Lesson:
#
# The keyword arguments are evaluted _when the function is defined, NOT when it is called.
# <codecell>