In python 2.x, dividing two integers returns an integer. However, if you use
from ___future___ import division
you can get a float value:
>>> 3/2
1
>>> from __future__ import division
>>> 3/2
1.5
>>>
>>>
>>> 3//2
1
>>> 4/3
1.3333333333333333
>>>
After the import
, you have to use //
instead of /
to do integer division. How can I revert the import
so that /
does integer division again?
__future__ module is a built-in module in Python that is used to inherit new features that will be available in the new Python versions.. This module includes all the latest functions which were not present in the previous version in Python. And we can use this by importing the __future__ module.
from __future__ import absolute_import means that if you import string , Python will always look for a top-level string module, rather than current_package.string . However, it does not affect the logic Python uses to decide what file is the string module.
Future statements tell the interpreter to compile some semantics as the semantics which will be available in the future Python version. In other words, Python uses from __future__ import feature to backport features from other higher Python versions to the current interpreter.
To remove an imported module in Python: Use the del statement to delete the sys reference to the module. Use the del statement to remove the direct reference to the module.
__future__
imports are special, and cannot be undone. You can read up on their behavior here.
Here are a few relevant portions:
A future statement is recognized and treated specially at compile time: Changes to the semantics of core constructs are often implemented by generating different code. It may even be the case that a new feature introduces new incompatible syntax (such as a new reserved word), in which case the compiler may need to parse the module differently. Such decisions cannot be pushed off until runtime.
...
A future statement typed at an interactive interpreter prompt will take effect for the rest of the interpreter session.
Since __future__
statements are handled at compile time as opposed to runtime, there is no runtime method for reverting the changed behavior.
With normal modules you can remove or unimport the module by deleting whatever you imported from the namespace, and deleting the entry for that import in sys.modules
(this second part may not be necessary depending on the use case, all it does is force the reloading of the module if it is imported again).
import
statements are local to the file you import in, so for example, if you have this file as example.py
:
from __future__ import division
print(1/2)
Then you load it in another file:
import example # prints 0.5 because `division` is imported in example.py
print(1/2) # prints 0 because `division` is not imported in this file
So if you want an import that's only used in some of your code, put that code in a separate file.
In the case you gave, I'm not sure it's helpful though. Why not just use //
when you need integer division?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With