How can I remove/unimport symbols from __future__ after importing them?

前端 未结 2 544
情书的邮戳
情书的邮戳 2020-12-10 17:13

In python 2.x, dividing two integers returns an integer. However, if you use

from ___future___ import division

you can get a float value:

2条回答
  •  长情又很酷
    2020-12-10 17:26

    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?

提交回复
热议问题