-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscope.py
More file actions
88 lines (58 loc) · 1.56 KB
/
Copy pathscope.py
File metadata and controls
88 lines (58 loc) · 1.56 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
# Inside Function
def myfunc():
x = 300
print(x)
myfunc()
# Inner Function The local variable can be accessed from a function within the function:
def myfunc():
x = 300
def myinnerfunc():
print(x)
myinnerfunc()
myfunc()
# GLobal Scope : A variable created outside of a function is global and can be used by anyone:
x = 30
def myfunc():
print(x)
myfunc()
print(x)
# Naming Variable :The function will print the local x, and then the code will print the global x:
x = 15
def myfunc():
x = 20
print(x)
myfunc()
print(x)
# Global Keyword :If you use the global keyword, the variable belongs to the global scope:
# x = 15
def myfunc():
global x
x = 20
myfunc()
print(x)
# NonLocal Keyword FUnction :If you use the nonlocal keyword, the variable will belong to the outer function:
def myfunc1():
x = "Jane"
def myfunc2():
nonlocal x
x = "hello"
myfunc2()
return x
print(myfunc1())
# LEGB Rule :Understanding the LEGB rule:
x = "global"
def outer():
x = "enclosing"
def inner():
x = "local"
print("Inner:", x)
inner()
print("Outer:", x)
outer()
print("Global:", x)
# Python Scope — Short Summary
# Scope = কোন জায়গা থেকে variable ব্যবহার করা যাবে।
# Local → function-এর ভিতরের variable
# Enclosing → nested function-এর বাইরের function-এর variable
# Global → পুরো program-এ ব্যবহারযোগ্য variable
# Built-in → Python-এর built-in names যেমন print(), len()