-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvariable scope & scope resolution.py
More file actions
62 lines (44 loc) · 1.08 KB
/
variable scope & scope resolution.py
File metadata and controls
62 lines (44 loc) · 1.08 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
#variable scope = where a variable is visiable and accessible
#scope resolution = sige la jerarquia (LEGB) Local => Enclosed => Global => Built-in
#las valirables a solamente funcionaria dentro de la funcion en la que aparecen. en el caso de fun2 quiera leer a saldra error.
# jerarquia local
def fun1():
a = 1
print(a)
def fun2():
b=2
print(b)
fun1()
fun2()
#ejemplo de una posible utilizacion de variable de una funcion en otra.
def happy_birthday(name, age):
print(f"Happy Birthday dear {name}")
print(f"You are {age} years old.")
def main():
name = "Roberto"
age = 26
happy_birthday(name,age)
main()
# jerarquia Enclosed
def fun1():
x = 1
def fun2():
print(x)
fun2()
fun1()
# jerarquia global
def fun1():
a = 1
print(x)
def fun2():
x=2#aunque estemos en jerarquia global, siempre que se asigne a un valor local este tendra prioridad.
print(x)
x = 3
fun1()
fun2()
#jerarquia built-in
from math import e
def fun1():
print(e)
#e = 7#siguiendo la jerarquia tomara el valor global en primera instancia
fun1()