[Python-Dev] nonlocal keyword in 2.x?

Guido van Rossum guido at python.org
Wed Nov 4 17:53:12 CET 2009

On Tue, Nov 3, 2009 at 11:06 PM, Lennart Regebro <regebro at gmail.com> wrote:
> I should maybe point out that although I'm generally +1 on
> backporting, I'm not specifically anything on backporting the nonlocal
> keyword. There are probably things that would help more from a
> compatibility standpoint than that.
> For example, from __future__ import unicode_literals doesn't switch
> the types. So this code will rais an assertion error in 2.6.
>>>> from __future__ import unicode_literals
>>>> assert isinstance("fghjkl", str)
> While it of course will run fine in 3.0. Now this has a fairly trivial
> workaround or two, but still.
> I also would really like to see a real port of the bytes class to 2.6,
> but I have a vague memory that there was some reason that wouldn't
> work.

There are many interfaces that return a str instance which should
become a bytes instance for code that is bytes-aware, but should stay
a str instance (which after all has the same representation) for code
that is not bytes-aware. In 3.x it's easy, such interfaces always
return bytes instances. But in 2.x, it would have to be context-aware,
and there are many reasons why that can't work. (E.g. an object could
be shared between two modules, one of which is bytes-aware, while the
other is not. Or a call could be made by a module that isn't
bytes-aware and the return value passed to a module that is
bytes-aware. Or vice versa. It's a mess unless *everyone* is
bytes-aware, which is the 3.x world.)

--Guido van Rossum (python.org/~guido)

More information about the Python-Dev mailing list