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

People, including me, know that there is something in Python called __future__ , and it appears in a number of modules that I read. And stupid people like me do not know why it is there and how / when to use it, even after reading the Python __future__ doc .

So, is everything explained by examples to demonstrate this?

I have a few quick answers that look right in terms of basic usage.

However, as well as to further understand how __future__ works:

I just realized one key thing that baffled me when I tried to understand it, that is, how is there something in the current release of python that will be released in a future version? and how will a program using the new function in a future version of python be compiled using the current version of python?

So, now I think that the current version already has some potential features that will be included in future releases - is that right? but functions are only available __future__ , because it has not yet become standard - am I right?

+611
python
Aug 16 2018-11-11T00:
source share
9 answers

With the inclusion of the __future__ module __future__ you can gradually get used to incompatible changes or to those that introduce new keywords.

For example, to use context managers, you had to do from __future__ import with_statement in 2.5, since the with keyword was new and should no longer be used as variable names. In order to use with as a Python keyword in Python 2.5 or later, you will need to use the import from above.

Another example

 from __future__ import division print 8/7 # prints 1.1428571428571428 print 8//7 # prints 1 

Without __future__ things like print expression will print 1 .

The inner difference is that without this import / maps to the __div__() method, and __div__() is used with it. (In any case, // calls __floordiv__() .)

By the way, print : print becomes a function in 3.x, losing its special property as a keyword. So the opposite is true.

 >>> print >>> from __future__ import print_function >>> print <built-in function print> >>> 
+334
Aug 16 2018-11-11T00:
source share

When you do it

 from __future__ import whatever 

In fact, you are not using the import statement, but a future statement . You are reading the wrong documents, since you are not actually importing this module.

Future statements are special - they change the order of analysis of your Python module, so they should be at the top of the file. They give a new or different meaning to words or characters in your file. From the docs:

The future statement is a directive for the compiler that a particular module must be compiled using the syntax or semantics that will be available in the specified future version of Python. The future operator is designed to facilitate the transition to future versions of Python that make incompatible changes to the language. It allows you to use new features for each module before release, in which this feature becomes standard.

If you really want to import the __future__ module, just do

 import __future__ 

and then access it as usual.

+176
Aug 16 '11 at 8:00
source share

__future__ is a pseudo __future__ that programmers can use to enable new language functions that are not compatible with the current interpreter . For example, expression 11/4 is currently evaluated as 2 . If the module in which it is running has enabled true division by doing:

from __future__ import division

expression 11/4 will be evaluated to 2.75 . __future__ importing the __future__ module and evaluating its variables, you can see when the new function was first added to the language and when it will become standard:

  >>> import __future__ >>> __future__.division _Feature((2, 2, 0, 'alpha', 2), (3, 0, 0, 'alpha', 0), 8192) 
+106
Feb 20 '13 at 20:38
source share

It can be used to use functions that will appear in newer versions, having an older version of Python.

for example

 >>> from __future__ import print_function 

allow you to use print as a function:

 >>> print('# of entries', len(dictionary), file=sys.stderr) 
+43
Aug 16 2018-11-11T00
source share

There are already several __future__ answers, but none of them contain a complete list of what the __future__ operator currently supports.

Simply put, the __future__ statement forces Python interpreters to take advantage of new language features.

It currently supports the following features:

nested_scopes :

Prior to Python 2.1, the following code would throw a NameError error :

 def f(): ... def g(value): ... return g(value-1) + 1 ... 

from __future__ import nested_scopes will enable this function.

generators :

Generator functions have been introduced, such as the one below, to maintain state between successive function calls:

 def fib(): a, b = 0, 1 while 1: yield b a, b = b, a+b 

division :

The classic split is used in versions of Python 2.x. This means that some division operators return a reasonable approximation to division ("true division"), while others return the word ("division by gender"). Starting in Python 3.0, true division is defined as x/y , while gender division is defined as x//y .

from __future__ import division forces the use of Python 3.0 style partitioning.

absolute_import :

Enables you to bracket multiple import statements. For example:

 from Tkinter import (Tk, Frame, Button, Entry, Canvas, Text, LEFT, DISABLED, NORMAL, RIDGE, END) 

Instead:

 from Tkinter import Tk, Frame, Button, Entry, Canvas, Text, \ LEFT, DISABLED, NORMAL, RIDGE, END 

Or:

 from Tkinter import Tk, Frame, Button, Entry, Canvas, Text from Tkinter import LEFT, DISABLED, NORMAL, RIDGE, END 

with_statement :

Adds a "with" statement as a keyword in Python to eliminate the need for try/finally . This is usually used when performing file I / O, for example:

 with open('workfile', 'r') as f: read_data = f.read() 

print_function :

Forcing the use of the print function call to the Python 3 style brackets instead of the print MESSAGE statement print MESSAGE style.

unicode_literals :

Introduces the literal syntax for the bytes object. This means that expressions such as bytes('Hello world', 'ascii') can simply be expressed as b'Hello world' .

generator_stop :

Replaces the use of the StopIteration exception used within the generator functions with a RuntimeError exception.

Another use not mentioned above is that the __future__ operator also causes the use of Python __future__ interpreters. 1+, since using an older version will throw a runtime exception.

Recommendations:

+25
Mar 11 '18 at 17:19
source share

Or is it how to say "Since this is Python v2.7, use this other" print "function, which was also added in Python v2.7, after it was added in Python 3. So my" print "will no longer be operators (for example, print "message"), but functions (for example, print ("message", options)). Thus, when my code runs in python 3, "print" will not break. "

AT

 from __future__ import print_function 

print_function is a module containing a new print implementation in accordance with its behavior in python v3.

This has more explanation: http://python3porting.com/noconv.html

+24
Nov 15
source share

One of the uses that I thought was very useful is print_function from the __future__ module.

In Python 2.7, I wanted characters from different print statements to print on the same line without spaces.

This can be done with a comma (",") at the end, but it also adds extra space. The above expression when used as:

 from __future__ import print_function ... print (v_num,end="") ... 

This will print the v_num value from each iteration on one line with no spaces.

+3
Jan 05 '16 at 15:14
source share

Explicit is better than implicit

 from __future__ import braces File "<stdin>", line 1 SyntaxError: not a chance 

[Courtesy: @mdeous]

0
Apr 11 '19 at 21:35
source share

After Python 3.0, printing is no longer just a statement, but its function. and is included in PEP 3105.

Also, I think the Python 3.0 package still has these special features. Let's look at its usability using the traditional Pyramid program in Python:

 from __future__ import print_function class Star(object): def __init__(self,count): self.count = count def start(self): for i in range(1,self.count): for j in range (i): print('*', end='') # PEP 3105: print As a Function print() a = Star(5) a.start() Output: * ** *** **** 

If we use the regular print function, we cannot achieve the same output, since print () has an extra line of newline. Therefore, each time the inner for loop is executed, it prints * on the next line.

-3
Apr 05 '16 at 11:33
source share



All Articles