-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscoping.py
63 lines (40 loc) · 1012 Bytes
/
scoping.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
x = "global"
def f():
x = "local_at_f_but_enclosing_for_nested_functions"
def g():
x = "some_local"
func = lambda x: x
print "in g:", x, func("a")
print "in f:", x
print "locals: ", locals()
print "globals: ", globals()
g()
f()
# there are no block scopes
for i in range(10):
pass
print "block scope panic 1: ", i
[x for x in range(10)]
print "block scope panic 2: ", x
def make_global():
global x
x = "set from f()"
class A:
b = 1
def __init__(self):
pass
def f(self):
self.b += 1 # 1. self.b does not exist => A.b is lookup result 2. result of A.b + 1 is stored in self.b
print self.b, A.b
def g(self):
A.b += 1
print self.b, A.b
print b
# be aware of shadowing
round = lambda s: 666 # shadows round function in __builtins__
print round(5.5)
# wow
__builtins__.True = False
print "True: ", True
__builtins__.raw_input = lambda s: "you lost"
print raw_input("Hihi")