[Python-ideas] Smoothing transition to Python 3

Random832 random832 at fastmail.com
Sat Jun 4 20:23:31 EDT 2016


On Sat, Jun 4, 2016, at 19:37, Guido van Rossum wrote:
> The bytes -> int behavior is widely considered a mistake. We're just
> not able to fix it without yet another round of layoffs ^W
> deprecations. And I'm not ready for that -- not even Python 4 should
> be allowed to change this unilaterally. Though maybe we could do
> something with a __future__ import.

I did have some thoughts (in the discussion a week or so ago about int
division returning float vs Fraction) on how a __future__ flag (which
have to be file scoped) effecting a change in the behavior of an
operator of a class could be implemented without a
__truediv__/__floordiv__ dichotomy intended to stick around forever.

In this case: Change the actual behavior of bytes.__getitem__ (direct
callers aren't important enough to matter) to return a 1-length bytes
instance. Add a compile flag (enabled by default, disabled by the future
import) which causes foo[bar] to become __oldgetitem__(foo, bar) where:

def __oldgetitem__(obj, i):
    if isinstance(obj, bytes) and not isinstance(i, slice):
        return ord(obj.__getitem__(i))
    else:
        return obj.__getitem__(i)

Or instead of literally calling a function, we could add another byte
code that directly implements this behavior. The question is how big the
performance hit is and how important is the performance of code that
doesn't use the future import, considering dynamic typing means every []
operator (except perhaps those statically knowable to be slices) goes
through this.


More information about the Python-ideas mailing list