-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGlobal_variable.py
54 lines (36 loc) · 1012 Bytes
/
Global_variable.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
#define global parameter
animal = 'fruitbat'
def print_global():
print('inside print_global: ', animal)
#print('at the top level: ', animal)
#print(print_global())
#If change global parameter
#def change_and_print_global():
# print('inside_change_and_print_global: ', animal)
# animal = 'wombat' #Change global paramer's value
# print('after the change: ', animal)
#
#print(change_and_print_global())
def change_local():
animal = 'wombat'
print('inside change_local: ', animal, id(animal))
print(change_local())
print(animal)
print(id(animal))
# Evince global
def change_and_print_global():
global animal # evince 'animal' parameter is global parameter
animal = 'wombat'
print('after the change: ', animal)
print(animal)
print(change_and_print_global())
print(animal)
# Using
animal = 'fruitbat' #global
def change_local():
animal = 'wombat' # local
print('locals:', locals())
print(animal, sep='\n')
print(change_local(), sep='\n')
print('globals:', globals(), sep='\n')
print(animal)