[New-bugs-announce] [issue12165] Does nonlocal include global?

Lukas Petru report at bugs.python.org
Tue May 24 07:57:37 CEST 2011

New submission from Lukas Petru <lukas.petru at seznam.cz>:

Can "nonlocal x" declaration also reference global x? I would assume that global scope automatically encloses all other scopes. But the nonlocal keyword seems to exclude global scope. This seems counter-intuitive. (maybe bug in implementation?)

For example:


def count(i):
    nonlocal x; x+=i; return x;

Checking this code in IDLE gives: no binding for nonlocal 'x' found. If this is what was intended, maybe it should be more clearly written in the documentation.

I am coming to Python from Lisp, so I am still learning Python's principles.

components: Interpreter Core
messages: 136722
nosy: Lukas.Petru
priority: normal
severity: normal
status: open
title: Does nonlocal include global?
type: behavior
versions: Python 3.2

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list