Python
Programming
Code Future
Python Tutorials
Coding Techniques

What is __future__ in Python used for and how/when to use it, and how it works

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

In Python, __future__ is a pseudo-module which programmers can use to enable new language features which are not compatible with the current interpreter. Essentially, it allows you to use features from future Python releases in your current projects.

Purpose of __future__

Python strives to maintain backward compatibility with older versions, but sometimes newer releases introduce features that could potentially break older code if implemented immediately. The __future__ module provides a way to gradually introduce these changes. By importing specific features from __future__, developers can test new features without waiting for the next Python release. This is particularly useful for smoothing the transition between Python 2 and Python 3.

How __future__ Works

To use features from the __future__ module, you should import them at the top of your Python script. The syntax looks like this:

python
from __future__ import feature_name

This import needs to be placed before any other code or imports (except for from __future__ import annotations in Python 3.7+ which can be placed after other imports related to type hints). Once a feature has been successfully imported from __future__, it becomes globally active for the remainder of that program.

Commonly Used Features

Here are some commonly imported features from __future__:

  1. division: Changes the division operator / to mean true division (e.g., 3 / 2 == 1.5). Without this import, / performs integer division where the decimals are discarded if both operands are integers.
  2. print_function: This feature makes the print syntax in Python 3 (where print is a function) mandatory, even in Python 2.x.
  3. unicode_literals: With this feature, all string literals are automatically treated as unicode strings.
  4. absolute_import: It changes the way how imports work to avoid conflicts between system modules and local modules if they share the same name.
  5. annotations: From Python 3.7 onwards, it allows postponing the evaluation of type annotations, which can be useful for type hinting.

When to Use __future__

Use __future__ imports in the following scenarios:

  • When preparing your code for an upcoming Python version, especially if backward compatibility across versions is a concern.
  • To maintain clean and modern code practices, leveraging the latest Python features.
  • To ensure consistent behavior of certain operations (like division) across different Python versions (Python 2.x vs 3.x).

Example Usage

For an example, in a project where both Python 2.x and Python 3.x support might be required, using print_function from __future__ can be helpful:

python
from __future__ import print_function

print("Hello, World!")

If running this script in Python 2 without the import would raise a SyntaxError because print in Python 2 is a statement, not a function.

Summary Table

FeatureDescriptionPython Version IntroducedCommon Use
divisionMakes / operator perform true division2.2To ensure consistent division behavior
print_functionMakes print a function2.6For compatibility with Python 3.x's print function
unicode_literalsTreats all string literals as unicode2.6Helps in handling text data consistently in Python 2.x and 3.x
absolute_importChanges the way imports are handled2.5Prevents name collision between system and local modules
annotationsAllows postponing the evaluation of type annotations3.7Useful for forward references and other advanced type hinting

Conclusion

The __future__ module in Python is a critical tool for ensuring that codebases are ready for future releases while maintaining compatibility with current and older versions. By allowing developers to opt-in to new features progressively, Python ensures that its evolution is both innovative and stable. By understanding and utilizing __future__, developers can write more robust and forward-compatible Python code.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions