the Gravity of Python 2

Chris Angelico rosuav at gmail.com
Mon Jan 6 21:28:39 EST 2014


On Tue, Jan 7, 2014 at 1:15 PM, Devin Jeanpierre <jeanpierreda at gmail.com> wrote:
> The other alternative is having + (etc.) do something different
> depending on what module it's in. It's not hard to do: add a condition
> to all places where Python automatically converts, and check the call
> stack to see what module you're in.

Currently, there are __add__ methods (and __radd__ but let's focus on
__add__) on a bunch of objects, which determine what happens when you
use the + operator.

class Foo(str):
    def __add__(self, other):
        if isinstance(other, unicode): return self + other.encode("cp500")
        return str.__add__(self, other)

What happens if you have the __future__ directive disabling
autoencoding on (a) the module in which this class is defined, (b) the
one in which the it was instantiated, (c) the one that actually uses
the +?

This is why I think it's getting magical. Far better to do this sort
of change on a per-application basis - maybe with a warning parameter
that you can enable when running your test suite, as has been
suggested (and in many cases implemented) for other 2-vs-3 problems.

ChrisA



More information about the Python-list mailing list