-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path02a-Scopes.py
179 lines (90 loc) · 1.74 KB
/
02a-Scopes.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
"""
# Scope;
-> It just says, what variables do i have access to...
"""
hello = "hello world"
print(hello)
# print(hello2) - error because, it's out of scope
def fun1():
hello_ = "Dear Happs"
print(hello_)
fun1()
# print(hello_) - error because of scope, it's wrong indentation
"""
# Scope Rules;
"""
a = 1 # - Global Scope
# 1
def confusion(): # - It has it's own little universe lol ;)
a = 5
return a
print(a)
print(confusion())
print(confusion())
print(a)
# 2
b = 3
def confusion1():
return a, b
print(confusion1())
"""
# Rules;
1- Start with local
2- Parent local
3- Global
4- built-in Python functions
"""
h = 1
def parent():
a = 21
def confusion3():
return a
return confusion1(), confusion3()
print(parent())
print(a)
def parent1():
def confusion4():
return sum
return confusion4()
print(parent1())
"""
# global Keyword;
"""
total = 0
# 1
def count():
global total # global keyword
total += 1
return total
count()
count()
print(count())
total1 = 0
# 2
def count1(total1):
total1 += 1
return total1
print(count1(count1(count1(total1)))) # better way
"""
nonlocal Keyword;
-> Redirects to parent local...(Remember Rules !)
"""
def outer():
x = "outer"
def inner():
nonlocal x # nonlocal Keyword
x = "nonlocal"
print("inner x ; ", x)
inner()
print("outer x ; ", x)
outer()
"""
Why do we need scope?
-> Because, we don't have infinity hardware resources, they are limited that's why, we have to make efficient programs.
-> To make code clean, efficient & reusable..
"""
"""
Python Exam;
-> Test-1; 24/25
-> Test-2;
"""