Skip to main content

Clean single-source support for Python 3 and 2

Project description

The future module helps run Python 3.x-compatible code under Python 2 with minimal code cruft.

The goal is to allow you to write clean, modern, forward-compatible Python 3 code today and to run it with minimal effort under Python 2 alongside a Python 2 stack that may contain dependencies that have not yet been ported to Python 3.

It is designed to be used as follows:

from __future__ import (division, absolute_import, print_function,
                        unicode_literals)
from future import *

followed by clean Python 3 code (with a few restrictions) that can run unchanged on Python 2.7.

On Python 3, the from future import * line is guaranteed to have no effect (i.e. no namespace pollution.) On Python 2 it shadows builtins to provide the Python 3 semantics. (See below for the explicit import form.)

For example:

# New iterable range object with slicing support
for i in range(10**11)[:10]:
    pass

# Other common iterators: map, reduce, zip
my_iter = zip(range(3), ['a', 'b', 'c'])
assert my_iter != list(my_iter)

# New simpler super() function:
class VerboseList(list):
    def append(self, item):
        print('Adding an item')
        super().append(item)

# These raise NameErrors:
# apply(), cmp(), coerce(), reduce(), xrange(), etc.

# This identity is restored. This is normally valid on Py3 and Py2, but
# 'from __future__ import unicode_literals' breaks it on Py2:
assert isinstance('happy', str)

# The round() function behaves as it does in Python 3, using "Banker's
# Rounding" to the nearest even last digit:
assert round(0.1250, 2) == 0.12

# input() is now safe (no eval()):
name = input('What is your name?')
print('Hello ' + name)

future also supports the standard library reorganization (PEP 3108) via import hooks, allowing standard library modules to be accessed under their Python 3 names and locations:

from future import standard_library_renames

import socketserver
import queue
import configparser
# and other moved modules

If you prefer explicit imports, the explicit equivalent of the from future import * line above is:

from future.common_iterators import zip, map, filter
from future.modified_builtins import (range, super, round, input)
from future.disable_obsolete_builtins import (apply, cmp, coerce,
        execfile, file, long, raw_input, reduce, reload, unicode,
        xrange, StandardError)
from future.str_is_unicode import str, python_2_unicode_compatible

See the docstrings for each of these modules for more info:

- future.standard_library_renames
- future.common_iterators
- future.modified_builtins
- future.disable_obsolete_builtins
- future.str_as_unicode

Credits

Author:Ed Schofield
Sponsor:Python Charmers Pty Ltd, Australia, and Python Charmers Pte Ltd, Singapore. http://pythoncharmers.com
Others:The super() and range() functions are derived from Ryan Kelly’s magicsuper module and Dan Crosta’s xrange module. The python_2_unicode_compatible decorator is from django.utils.encoding.

Project details


Release history Release notifications

History Node

0.16.0

History Node

0.15.2

History Node

0.15.1

History Node

0.15.0

History Node

0.14.3

History Node

0.14.2

History Node

0.14.1

History Node

0.14.0

History Node

0.13.1

History Node

0.13.0

History Node

0.12.4

History Node

0.12.3

History Node

0.12.2

History Node

0.12.1

History Node

0.12.0

History Node

0.11.4

History Node

0.11.3

History Node

0.11.2

History Node

0.11.1

History Node

0.11.0

History Node

0.10.2

History Node

0.10.1

History Node

0.10.0

History Node

0.9.0

History Node

0.8.2

History Node

0.8.1

History Node

0.8.0

History Node

0.7.0

History Node

0.6.0

History Node

0.5.2

History Node

0.5.1

History Node

0.5.0

History Node

0.4.1

History Node

0.4.0

History Node

0.3.5

History Node

0.3.4

History Node

0.3.3

History Node

0.3.2

History Node

0.3.1

History Node

0.3.0

This version
History Node

0.2.0

History Node

0.1.0

History Node

0.0.3

History Node

0.0.2

History Node

0.0.1

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
future-0.2.0.tar.gz (28.9 kB) Copy SHA256 hash SHA256 Source None Jul 26, 2013

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging CloudAMQP CloudAMQP RabbitMQ AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page