DEV Community

Cover image for Python __future__ module
Mohamad Ashraful Islam
Mohamad Ashraful Islam

Posted on

Python __future__ module

Originally posted on Ashraful's Blog

❓ What is future module btw?

  • future is python built-in module.
  • It serves three purposes,
  • To avoid confusing existing tools that analyze import statements and expect to find the modules they’re importing.
  • To ensure future statements run under release prior to 2.1 at least yield runtime exceptions
  • future will allow you to use a feature from the future.

I know it sounds crazy for the first time you heard. But it's real. Let's dig into this.

🔥 More about future.

future module introduced from python 2.1. On the source of cpython's __future__.py there is defined each future feature like following,

FeatureName = _Feature(OptionalRelease, MandatoryRelease, CompilerFlag)
Enter fullscreen mode Exit fullscreen mode

Where, normally, OptionalRelease is less than MandatoryRelease, and both are 5-tuples of the same form as sys.version_info

sys.version_info(major=3, minor=8, micro=1, releaselevel='final', serial=0)
Enter fullscreen mode Exit fullscreen mode

💥 Okay! Enough talk. Let's see the usage.

Suppose, you are using python2(Though python 2 is dead. Just take it an example). You want to use the print function from python3. But how you can do it? Here comes the future module.

Python 3 Console

>>>
>>> print('Hello', 'World', sep=', ', end='\n')
Hello, World
>>>
>>>
Enter fullscreen mode Exit fullscreen mode

In python2 you need to do the following,

>>>
>>> from __future__ import print_function
>>>
>>> print('Hello', 'World', sep=', ', end='\n')
Hello, World
>>>
Enter fullscreen mode Exit fullscreen mode

Here, I have imported print_fuction from future module. Then it's working as expected from python3. I hope you got it.

😳 Available future functions

nested_scopes
generators
division
absolute_import
with_statement
print_function
unicode_literals
barry_as_FLUFL
generator_stop
annotations

See the source here

Good Luck 😃

Discussion (0)