20
votes

In order to transition to python 3, I am trying to understand writing python 2 and python 3 compatible codes. The following code is from python-future.org and illustrates a way to construct an iterator that would be compatible with both versions of python.

from builtins import object

class Upper(object):
    def __init__(self, iterable):
        self._iter = iter(iterable)
    def __next__(self):      # Py3-style iterator interface
        return next(self._iter).upper()  # builtin next() function calls
    def __iter__(self):
        return self

itr = Upper('hello')
assert next(itr) == 'H'      # compatible style
assert list(itr) == list('ELLO')

The code runs fine in python 2, but to my surprise, if i remove the import statement then i get an error TypeError: Upper object is not an iterator. I often derive my custom classes from object but i have never imported it from builtins. Why does simply importing object change the behaviour of the code?

1
Python 2 requires builtins to be pip-installed. It's designed for writing code compatible on Python 2 and 3. - Aaron Christiansen
When I run this code in Python 2, I get ImportError: No module named builtins. There is a module named __builtin__, and I can do from __builtin__ import object, but it makes no difference, I get the TypeError: Upper object is not an iterator either way. Which makes sense, because in Python 2 you implement next, not __next__, to be an iterator. I think the code sample you are reading is just wrong. Could you please post a direct link to it? Also please tell us exactly which versions of Python 2 and 3 you are testing with. - zwol
So there is a difference between the object available in the default namespace and the one that is imported from builtins? - deepak
@OrangeFlash81 Oh, builtins is another forward-compatibility bolt-on like six? I'm not finding it on PyPI, where do you get it? - zwol

1 Answers

28
votes

You are (indirectly) importing from the future.builtins module; it provides a custom object baseclass that adds forward-pointing special names.

In Python 2, iterators must have a next() method (as well as __iter__); this method was renamed to __next__ in Python 3. By not using the future.builtins.object version you are simply missing the next -> __next__ alias provided in Python 2.

See the source code for future.types.newobject.py:

def next(self):
    if hasattr(self, '__next__'):
        return type(self).__next__(self)
    raise TypeError('newobject is not an iterator')

Note that builtins will return standard built-in objects if you are running Python 3, the module only returns shims like these for Python 2.

You could simply add that same alias yourself:

class Upper(object):
    def __init__(self, iterable):
        self._iter = iter(iterable)

    def __iter__(self):
        return self

    def __next__(self):      # Py3-style iterator interface
        return next(self._iter).upper()  # builtin next() function calls
    next = __next__          # Py2 alias