-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScope.py
More file actions
81 lines (55 loc) · 1.61 KB
/
Scope.py
File metadata and controls
81 lines (55 loc) · 1.61 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
"""
* we've discussed scope a bit in the past,
but python's scope rule can sometimes confuse beginners, so let's quickly go ever
the key rulse of python's scope.
* python scope follows the LEGB rule:
- L:
Local:
names assigned in any way within a function (def or lambda), and
not declared global in that function.
- E:
Enclosing function locals: (EFLs)
name in the local scop of any and all enclosing function (def or lambda),
from inner to outer.
- G:
Global: (module)
names assigned at the top-level of a module file, or declared global in a def
within the file.
- B:
Builte-in: (python)
names preassigned in the built-in names module:
open, range, syntaxerror, ...
"""
x = 25
def my_func():
x = 50
return x
print(x) # 25
print(my_func()) # 50
print(my_func())
print(x)
# local
lambda x:x**2
# Enclosing function locals
name = 'this is a global name!'
def greet():
name = 'Ruhollah'
def hello():
return 'hello : {}'.format(name)
return hello() # built-in
print(greet())
def greet(inOrout = 'out'):
name = 'Ruhollah'
def hello():
global name
return 'hello : {}'.format(name)
if inOrout == 'in':
return 'hello : {}'.format(name)
return hello()
print(greet('out'))
def change():
global name
name = 'change global name!'
print('before function call: ',name)
change()
print('after function call: ',name)