ある名前空間の外の変数に代入したい時に使います。
| 1 2 3 4 5 6 7 8 9 10 | >>> def outfunc(): ...     o = "これが" ...     def infunc(): ...         nonlocal o ...         o = "変わった" ...     infunc() ...     print(o) ... >>> outfunc() 変わった | 
パイソンカーニバル。Pythonをはじめ、TypeScript、WindowsやMacなど、開発を楽しむ祭典。
ある名前空間の外の変数に代入したい時に使います。
| 1 2 3 4 5 6 7 8 9 10 | >>> def outfunc(): ...     o = "これが" ...     def infunc(): ...         nonlocal o ...         o = "変わった" ...     infunc() ...     print(o) ... >>> outfunc() 変わった |