‘global’
keyword is used to create/change a global variables (You can declare functions,
classes etc. also) from local context.
test.py
def function1(): global data data="Hello World" def function2(): print(data) function1() function2()
$ python3 test.py Hello World
Observe
‘test.py’, even though data is declared in function1, it is accessed by
function2. It is because, data is declared in global scope.
References
No comments:
Post a Comment