#!/usr/bin/env python
# coding: utf-8
VAR1 = 111
VAR2 = 222
VAR3 = 333
def func() :
VAR2 = 444
global VAR3
VAR3 = 555
print('--- in func() ---')
print('VAR1=' + str(VAR1) + ' そのまま参照(global変数)')
print('VAR2=' + str(VAR2) + ' 444 を上書き(local変数)')
print('VAR3=' + str(VAR3) + ' global宣言して 555 を上書き(global変数)')
print('--- (global in func()) ---')
print([item for item in globals().items() if not item[0].startswith('__')])
print('--- (local in func()) ---')
print([item for item in locals().items() if not item[0].startswith('__')])
print('***** START STATUS *****')
print('VAR1=' + str(VAR1))
print('VAR2=' + str(VAR2))
print('VAR3=' + str(VAR3))
print('************************\n')
func()
print('\n***** END STATUS *******')
print('VAR1=' + str(VAR1))
print('VAR2=' + str(VAR2))
print('VAR3=' + str(VAR3))
print('************************')
$ ./scope.py ***** START STATUS ***** VAR1=111 VAR2=222 VAR3=333 ************************ --- in func() --- VAR1=111 そのまま参照(global変数) VAR2=444 444 を上書き(local変数) VAR3=555 global宣言して 555 を上書き(global変数) --- (global in func()) --- [('VAR1', 111), ('VAR3', 555), ('VAR2', 222), ('func', <function func at 0x102274050>)] --- (local in func()) --- [('VAR2', 444)] ***** END STATUS ******* VAR1=111 VAR2=222 VAR3=555 ************************