PY
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
#main difference seems to be the omission of the global keyword
def f():
#do not have to use global keyword
f.count += 1
print('dot:', f.count)
#have to use global keyword
global count
count += 1
print('normal:', count)
# variable declaration outside of f
# both types have to be declared before function is called or error will be raised
f.count = 0 # dot separated variable
count = 0 # normal variable have to be declared before function call
f()
f()
f()
print("outside of f: ", f.count, count)
#other function also using the globals
def b():
#dot separated variable still accessible by other functions
f.count -= 1
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run