Repository navigation
Expand file tree
/
Copy path062_function_scope.py
More file actions
69 lines (42 loc) · 2.36 KB
/
Copy path062_function_scope.py
File metadata and controls
69 lines (42 loc) · 2.36 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
# Lesson 062 - Function Scope
# Video: https://www.youtube.com/watch?v=VQHLn1wuDBw
# --------------------
# -- Function Scope --
# --------------------
x = 1 # Global Scope
def one():
return f"Print Variable From Function Scope {x}"
print(f"Print Variable From Global Scope {x}") # Print Variable From Global Scope 1
print(one()) # Print Variable From Function Scope 1
# # =======================================================================
x = 1 # Global Scope
def two():
x = 2 # Local Scope -> هذا المتغير موجود داخل الدالة فقط، ولا يغير المتغير الخارجي
return f"Print Variable From Function Scope {x}"
print(f"Print Variable From Global Scope {x}") # Print Variable From Global Scope 1
print(two())
# # =======================================================================
# x = 1 # Global Scope
def three():
x = 4
return f"Print Variable From Function Scope {x}"
print(f"Print Variable From Global Scope {x}") # Print Variable From Global Scope 1
# طبع 1 لأن المتغير الخارجي معرف في الأعلى، أما القيمة 4 فهي خاصة بالدالة فقط
# لو لم يكن هناك متغير خارجي أصلا، لظهر خطأ NameError
print(three()) # Print Variable From Function Scope 4
# =======================================================================
def four():
global x
x = 2 # -> عرفت المتغير إكس ع إنو متغير عالمي على مستوى البرنامج
return f"Print Variable From Function Scope {x}"
def five():
# x = 10
return f"Print Variable From Function Scope {x}"
# print(f"Print Variable From Global Scope {x}") # Print Variable From Global Scope 1
# قبل استدعاء الدالة four القيمة ما زالت 1، والتغيير إلى 2 يحدث فقط بعد استدعائها
print(four()) # Print Variable From Function Scope 2
print(f"Print Variable From Global Scope {x}") # Print Variable From Global Scope 2
print(five()) # Print Variable From Function Scope 2
# لو فعلنا السطر x = 10 داخل الدالة five سيطبع 10
# لأن الدالة تبحث عن المتغير داخلها أولا، ثم في الخارج
print(five()) # Print Variable From Function Scope 2 -> Globalبروح وبدور في ال Functionداخل ال xفي حال ما عرفنا ال