[Python-Dev] Whatever happened to 'nonlocal x = y'?

Random832 random832 at fastmail.com
Fri Jan 5 13:48:32 EST 2018

On Fri, Jan 5, 2018, at 10:47, Guido van Rossum wrote:
> I don't recall (though someone with more time might find the discussion in
> the archives or on the tracker). It was never implemented and I think it
> shouldn't be. So we might as well update the PEP. It wouldn't be
> particularly useful, since (by definition) the function that declares the
> nonlocal variable is not its owner, and hence it's unlikely to make sense
> to initialize it here. The same reasoning applies to global BTW.

I'm not so sure...

The only situation in which you're *required* to declare a nonlocal/global variable, after all, is if you intend to assign it - a name that you never assign is presumed to be non-local. The description in the PEP also applies to augmented assignments, and "global some_counter; some_counter += 1" is certainly a pattern I've needed in the past.

The PEP also quotes you as endorsing this for global. https://mail.python.org/pipermail/python-3000/2006-November/004166.html

More information about the Python-Dev mailing list