-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscope.py
More file actions
133 lines (99 loc) · 4.3 KB
/
Copy pathscope.py
File metadata and controls
133 lines (99 loc) · 4.3 KB
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
124
125
126
127
128
129
130
131
132
133
# all variables declared inside has scope of that function
# it does not pollute outside scope
# arguments are also live in function's scope
def myFuncA( num ):
result = num ** num
return result
print( "myFuncA(3) => ", myFuncA( 3 ) )
# print( num ) # NameError: name 'num' is not defined
# print( result ) # NameError: name 'result' is not defined
# variables in python are lexically scoped
firstName = "John"
def myFuncB( lastName ):
print("myFuncB: firstName, lastName => ", firstName, lastName )
age = 26
def myInnerFuncB( gender ):
email = "john@email.net"
print("myInnerFuncB: firstName, lastName, age, gender, email => ", firstName, lastName, age, gender, email )
myInnerFuncB( "Male" )
myFuncB( "Doe" )
# python can not override variable from the outer scope
# it always create new variable in function scope
firstName = "John"
def myFuncC( lastName ):
firstName = "Mike"
gender = "Male"
age = 26
def myInnerFuncC( gender ):
age = 27
print("myInnerFuncC-scope: gender => ", gender )
print("myInnerFuncC-scope: age => ", age )
myInnerFuncC( "Female" )
print( "myFuncC-scope: firstName => ", firstName )
print( "myFuncC-scope: lastName => ", lastName )
print( "myFuncC-scope: gender => ", gender )
print( "myFuncC-scope: age => ", age )
myFuncC( "Doe" )
print( "global-scope: firstName => ", firstName )
# variables defined on file level scope are called as `global` variables
# variables defined inside a function are called as `local` variables
# to override a global variable, we need to tell python, it is a global variable using `global` keyword
fruit = "Mango"
drink = "Vanilla Shake"
def myFuncD():
global fruit # define fruit as global
fruit = "Apple" # change its value
drink = "Virgin Mojito" # creates a local variable
print("myFuncD-scope: fruit => ", fruit)
print("myFuncD-scope: drink => ", drink)
myFuncD()
print("global-scope: fruit => ", fruit)
# if a global variable is not defined, using `global` variable, it can be created
def myFuncE():
global car # define fruit as global, but doesn't create one
car = "Mercedes" # create global variable if not present and assign a value
print("myFuncE-scope: car => ", car)
myFuncE()
print("global-scope: car => ", car)
# inner python function can also use `global` keyword
def myFuncF():
country = "India" # creates a local variable
def myInnerFuncF():
global country # define country as global variable
country = 'Denmark' # assign a value to global variable
print("myInnerFuncF-scope: country => ", country)
myInnerFuncF()
print("myFuncF-scope: country => ", country)
myFuncF()
print("global-scope: country => ", country)
# since a new variable is always created inside an inner function
# when we assign a value to a variable if it is coming from lexical scope
# `global` is useful to use variables from global scope
# to use variable from lexical scope, we need to use `nolocal` keyword
# warning: unline `global` keyword, `nolocal` does not create a variable in any of the outer scopes
# warning: nonlocal does not work for variables coming from global scope, have to use `global`
def myFuncG():
color = 'red'
taste = 'sweet'
mood = 'happy'
def myInnerFuncG():
nonlocal color # comes from myFuncG-scope
color = 'green'
taste = 'sour'
def myNestedInnerFuncG():
nonlocal mood # comes from myFuncG-scope
mood = 'sad'
# color comes from myFuncG-scope as well
print("myNestedInnerFuncG-scope: color, taste, mood => {}, {}, {}".format( color, taste, mood ))
myNestedInnerFuncG()
print("myInnerFuncG-scope: color, taste, mood => {}, {}, {}".format( color, taste, mood ))
myInnerFuncG()
print("myFuncG-scope: color, taste, mood => => {}, {}, {}".format( color, taste, mood ))
myFuncG()
# like JavaScript, python hoists variables in a function scope without assiging any alue
# hence below example would fail
originalValue = 2
def myFuncH():
originalValue = originalValue * 2 # multiply global value by 2 and create local variable
print( "myFuncH-scope: originalValue => ", originalValue )
myFuncH() # UnboundLocalError: local variable 'originalValue' referenced before assignment