0 votes
1 view
in Python by (19k points)

People including me know there is something in Python called __future__ and it appears in quite a few modules I read. And the dull people like me don't know why it's there, and how/when to use it, even after reading the Python's __future__ doc.

So any explains with examples to demonstrate it?

I have got a few answers quickly, which look all correct, in terms of the basic usage.

However and also for further understanding of how __future__ works:

I just realized one key thing that was confusing me when I tried to understand it, that is, how a current python release includes something that will be released in a future release? and how can a program use a new feature in a future Python release be compiled successfully by the current python release?

So, I guess now that, the current release has already packaged some potential features that will be included in future releases - is this right? but the features are available only by __future__, that is because it doesn't become standard yet - am I right?

 

1 Answer

0 votes
by (50.5k points)

When you include  __future__ module, you can slowly be habitual to incompatible changes or to such ones introducing new keywords and operators. Python does not allow anyone to implement new operators or keywords except __future__ module.

So you will import the future module as follows:-

from __future__ import with_statement

An example that illustrates the above concept:-

from __future__ import division 

print(8/7) 

print(8//7) 

image

So why we are using the __future__ module:-

If we would have not used the __future__  module both print statements would have printed 1 in Python 2 version.

In Python 3 print() is a function so it does not need to include future module without including the future module it will give the desired output.

print 8/7 

print 8//7 

image

...