python:from __future__ import 的含义

__future__模块是为了在当前版本python中使用新python3.x特性而加入的语法。

这个模块里包含了(对于当前版本而言)没有加入当前版本python的新特性。

使用方法如下,在文件开头书写下面语句,否则会起SyntaxError

from __future__ import *

python的新特性会在某一个版本之后被强制地加入发行版本,但是没被强制加入发行版本的特性是新旧兼容的。这个会在下文中提到。一些经典的特性见下表:

在这里插入图片描述

想查看有哪些新特性,可以使用下面的代码:

# python 3.8.12
>>> import __future__
>>> print(__future__.all_feature_names)
['nested_scopes', 'generators', 'division', 'absolute_import', 'with_statement', 'print_function', 'unicode_literals', 'barry_as_FLUFL', 'generator_stop', 'annotations']

下面是几个例子:

1. print_function

在python2中,print函数长这样:

# python2
print "hello world"

但是在python3中,print函数长这样:

# python3
print("hello world")

在python2中,两种写法都是可以直接用的:

# python2
>>> print "hello world"
hello world
>>> print("hello world")
hello world

但是一旦使用了from __future__ import print_function语句,python2的语法就不再生效:

from __future__ import print_function

>>> print("hello world")
hello world
>>> print "hello world"
  File "/home/0a506b14e8975b8a788846c5356abb76.py", line 4
    print "Hello world"
                      ^
SyntaxError: invalid syntax

2. division

python2中的除法/是地板除,即除数向下取整。但是python3里的除法是精确除法:

# python2
>>> print 7 / 5
1
>>> print -7 / 5
-2
# In below python 2.x code, division works
# same as Python 3.x because we use  __future__
   
>>> from __future__ import division     
>>> print 7 / 5
1.4
>>> print -7 / 5
-1.4
  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
引用\[1\]: 如果你想使用import string来引入系统自带的string.py,而不是当前目录下的string.py,你可以使用from __future__ import absolute_import这句话。这样,你就可以使用import string来引入系统自带的string.py,同时使用from pkg import string来引入当前目录下的string.py。\[1\] 引用\[2\]: 这句from __future__ import absolute_import的作用是引入"绝对引入"。在python2.4或之前的版本中,如果你在代码中写入import string,python会先查找当前目录下是否有string.py,如果有,则引入该模块。但如果你想使用系统自带的标准string.py,而不是当前目录下的string.py,就需要使用from __future__ import absolute_import这句话。这样,你就可以使用import string来引入系统自带的标准string.py,同时使用from pkg import string来引入当前目录下的string.py。\[2\] 所以,当你在python2.4之后的环境下运行代码时,不需要加上from __future__ import absolute_import这句话。但如果你在python2.4或之前的环境下运行代码,并且想要引入系统自带的标准string.py,就需要加上这句话。\[1\]\[2\] #### 引用[.reference_title] - *1* *2* [【Python】from __future__ import absolute_import的作用](https://blog.csdn.net/qq_15971883/article/details/121527294)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [from __future__ import absolute_import的作用](https://blog.csdn.net/u012206617/article/details/106493749)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

PenguinLeee

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值