python的模块future用法实例解析
2023-09-11 14:17:11 时间
计算机的知识太多了,很多东西就是一个使用过程中详细积累的过程。最近遇到了一个很久关于future的问题,踩了坑,这里就做个笔记,免得后续再犯类似错误。
future的作用:把下一个新版本的特性导入到当前版本,于是我们就可以在当前版本中测试一些新版本的特性。说的通俗一点,就是你不用更新python的版本,直接加这个模块,就可以使用python新版本的功能。 下面我们用几个例子来说明它的用法:
python 2.x print不是一个函数,不能使用help. python3.x print是一个函数,可以使用help.这个时候,就可以看一下future的好处了:
代码:
# python2 #from __future__ import absolute_import, division, print_function #print(3/5) #print(3.0/5) #print(3//5) help(print)
运行结果:
➜ future git:(master) ✗ python future.py File "future.py", line 8 help(print) ^ SyntaxError: invalid syntax
报错了,原因就是python2 不支持这个语法。
上面只需要把第二行的注释打开:
1 # python2 2 from __future__ import absolute_import, division, print_function 3 4 5 #print(3/5) 6 #print(3.0/5) 7 #print(3//5) 8 help(print)
结果如下,就对了:
Help on built-in function print in module __builtin__: print(...) print(value, ..., sep=' ', end='\n', file=sys.stdout) Prints the values to a stream, or to sys.stdout by default. Optional keyword arguments: file: a file-like object (stream); defaults to the current sys.stdout. sep: string inserted between values, default a space. end: string appended after the last value, default a newline.
另外一个例子:是关于除法的:
# python2 #from __future__ import absolute_import, division, print_function print(3/5) print(3.0/5) print(3//5) #help(print)
结果:
➜ future git:(master) ✗ python future.py 0 0.6 0
把编译宏打开,运算结果:
➜ future git:(master) ✗ python future.py 0.6 0.6 0
看看,python3.x的语法可以使用了。
有了这两个例子,估计你对future的用法就清晰了吧。
相关文章
- [Python] 专题五.列表基础知识 二维list排序、获取下标和处理txt文本实例
- Python之——python-nmap的安装与常用方法说明
- Python MySQLdb模块连接操作mysql数据库实例_python
- python自动化测试之连接几组测试包实例
- Python命令行参数解析模块getopt使用实例
- python使用cPickle模块序列化实例
- 【原创】Python 工具 cheat
- Python命令行参数解析模块getopt使用实例
- Python实现的选择排序算法原理与用法实例分析
- 用python实现简单EXCEL数据统计的实例
- python使用cPickle模块序列化实例
- Python 在问答频道中刷题积累到的小技巧(二)
- 浅析算法的时间复杂度和空间复杂度 (C++/python双语实例)
- Python的IDE:基于Eclipse/MyEclipse软件的PyDev插件配置python的开发环境(不同python项目加载不同版本的python)—从而实现Python编程图文教程之详细攻略
- Python语言学习:Python语言学习之python包/库package的简介(模块的封装/模块路径搜索/模块导入方法/自定义导入模块实现华氏-摄氏温度转换案例应用)、使用方法、管理工具之详细攻略
- 已解决2. Set PROTOCOL_BUPFERS_PYTHON_iMPLEMENTATION=python (but this will use pure-Python parsing and w
- Python编程:pyenv管理多个python版本环境
- Python使用redis介绍
- python中的lambda表达
- python里使用正则表达式的非捕获模式实例二
- python基础===Python 迭代器模块 itertools 简介
- 【异常】前端ERR! stack Error: Can‘t find Python executable “python“, you can set the PYTHON env variable.
- 第34讲:Python中集合对象的概念以及基本使用