QBoard » Advanced Visualizations » Viz - Python » What is __future__ in Python used for and how/when to use it, and how it works

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

  • __future__ frequently appears in Python modules. I do not understand what __future__ is for and how/when to use it even after reading the Python's __future__ doc.

    Can anyone explain with examples?

    A few answers regarding the basic usage of __future__ I've received seemed correct.

    However, I need to understand one more thing regarding how __future__ works:

    The most confusing concept for me is how a current python release includes features for future releases, and how a program using a feature from a future release can be be compiled successfully in the current version of Python.

    I am guessing that the current release is packaged with potential features for the future. However, the features are available only by using __future__ because they are not the current standard. Let me know if I am right.
      December 21, 2021 2:02 PM IST
    0
  • It can be used to use features which will appear in newer versions while having an older release of Python.

    For example

    >>> from __future__ import print_function
    

     

    will allow you to use print as a function:

    >>> print('# of entries', len(dictionary), file=sys.stderr)
    
      January 1, 2022 2:04 PM IST
    0