Python基础之--from __future__ import unicode_literals作用
来源:互联网 发布:智能语音助手软件 编辑:程序博客网 时间:2024/05/22 15:51
① Python的每个新版本都会增加一些新的功能,或者对原来的功能作一些改动。有些改动是不兼容旧版本的,也就是在当前版本运行正常的代码,到下一个版本运行就可能不正常了
② 从Python 2.7到Python 3.x就有不兼容的一些改动,比如2.x里的字符串用'xxx'
表示str,Unicode字符串用u'xxx'
表示unicode,而在3.x中,所有字符串都被视为unicode,因此,写u'xxx'
和'xxx'
是完全一致的,而在2.x中以'xxx'
表示的str就必须写成b'xxx'
,以此表示“二进制字符串”。
要直接把代码升级到3.x是比较冒进的,因为有大量的改动需要测试。相反,可以在2.7版本中先在一部分代码中测试一些3.x的特性,如果没有问题,再移植到3.x不迟。
③ Python提供了__future__
模块,把下一个新版本的特性导入到当前版本,于是我们就可以在当前版本中测试一些新版本的特性。举例说明如下:
④ 为了适应Python 3.x的新的字符串的表示方法,在2.7版本的代码中,可以通过unicode_literals
来使用Python 3.x的新的语法:在python3中默认的编码采用了unicode, 并取消了前缀u. 如果代码要兼容python2/3
所以,在使用Django时,在models.py中导入-from __future__ import unicode_literals目的在于做版本的兼容性
1 0
- Python基础之--from __future__ import unicode_literals作用
- from __future__ import unicode_literals
- from __future__ import 作用
- Python 基础 —— from __future__ import
- python from __future__ import division
- python from __future__ import division
- python from __future__ import division
- Python from __future__ import division
- python的import __future__作用
- Python自然语言处理小问题之from __future__ import division
- from __future__ import absolute_import的作用
- from __future__ import absolute_import的作用
- python中的python from __future__ import division
- 关于python 中的__future__模块(from __future__ import ***)
- from __future__ import absolute_import
- from __future__ import absolute_import
- from __future__ import division
- from __future__ import print_function
- Linux下搭建git服务器
- 求职准备415
- 【HTML】简单的公司主页
- MyBatis--查询缓存--增删改对内置二级缓存的影响
- C++中 int转string 方法
- Python基础之--from __future__ import unicode_literals作用
- JDBC--学习笔记(三)数据源与连接池
- 基于tensorflow实现图像风格的变换
- 同余——HDU 4596
- 《论美国的民主》读后感
- Ubuntu+Eclipse+SparkStreaming
- Centos7安装Logstash及基本配置
- muduo库的消息循环机制
- js给KindEditor的textarea框赋值