-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_36_Global_keywords.py
More file actions
38 lines (29 loc) · 841 Bytes
/
_36_Global_keywords.py
File metadata and controls
38 lines (29 loc) · 841 Bytes
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
a=10 # global variable if not defined in global - error
print('out a=',id(a))
def fun1():
def some():
# a=15 # local variable
global a # specify as global - can change value of a - no local variable more
print('some before a=', id(a))
a=15
print('some after a=', id(a))
print('in fun:',a)
print('out-before-fun a=',id(a),a) #10
some()
print('out-after-fun a=',id(a),a) #15
# fun1()
#
# global and local variable together
def fun2():
def some2():
a=9
print('some2 a=', id(a))
x=globals()['a']
print('some x=', id(x))
print('some2',a,x)
globals()['a']=20 #will change value of ouside a without changing local
print('some2 a=', id(a))
some2()
print('outside:',a)
print('out a=',id(a))
fun2()