Python语言学习:解决python版本升级问题集合(python2系列→Python3系列)导致错误的总结集合

目录

Python版本升级的原因

Text and binary data in Python 2 are a mess

Python版本升级问题及其解决方法


Python版本升级的原因

Python团队核心开发人员Brett Cannon用一篇文章解释了 Why Python 3 exists,部分内容见下边

Text and binary data in Python 2 are a mess

Quick, what does the following literal represent semantically?

'abcd'

If you're a Python 3 user you would say it's the string consisting of the letters "a", "b", "c", and "d" in that order.

If you're a Python 2 user you may have said the same thing. You may have also said it was the bytes representing 97, 98, 99, and 100. And it's the fact that there are two correct answers in Python 2 for what the str object represents that led to changing the language so that the single Python 3 answer was the only answer.

The Zen of Python says that "there should be one -- and preferably only one -- obvious way to do it". Having literals in the language that could represent either textual data or binary data was a problem. If you read something from the network, for instance, you would have to be very careful to either say the str object you returned represented binary data or textual data because there was no way to know once the object left your control. Or you might have a bug in your code where you were meant to translate that str object into textual data -- or something else entirely -- but you messed up and accidentally skipped that step. With the str object potentially represent two different semantic types it was hard to notice when this kind of slip-up occurred.

Now you might try and argue that these issues are all solvable in Python 2 if you avoid the str type for textual data and instead relied upon the unicode type for text. While that's strictly true, people don't do that in practice. Either people get lazy and don't want to bother decoding to Unicode because it's extra work, or people get performance-hungry and try to avoid the cost of decoding. Either way it's making an assumption that you will code well enough to not mess up, and we all know that we are fallible human beings who are in fact not perfect. If people's hopes of coding bug-free code in Python 2 actually panned out then I wouldn't consistently hear from basically every person who ports their project to Python 3 that they found latent bugs in their code regarding encoding and decoding of text and binary data.

This point of avoiding bugs is a big deal that people forget. The simplification of the language and the removal of the implicitness of what a str object might represent makes code less bug-prone. The Zen of Python points out that "explicit is better than implicit" for a reason: ambiguity and implicit knowledge that is not easily communicated code is easy to get wrong and leads to bugs. By forcing developers to explicitly separate out their binary data and textual data it leads to better code that has less of a chance to have a certain class of bug.

Python版本升级问题及其解决方法

1、
Python2系列:NameError: name 'raw_input' is not defined
Python3系列:python3.0版本后用input替换了raw_input

2、

Python2系列:import urllib2
Python3系列:import urllib.request as urllib2

3、

Python2系列:import thread
Python3系列:import _thread as thread

4、

Python2系列:except Exception,e:
Python3系列:except Exception as e:

5、

Python2系列:xrange
Python3系列:range

6、

Python2系列:unichr(i)
Python3系列:chr(i)

7、

Python2系列:
Python3系列:

8、

Python2系列:
Python3系列:

9、

Python2系列:
Python3系列:

10、

Python2系列:
Python3系列:

相关文章
成功解决NameError: name 'apply' is not defined
成功解决ModuleNotFoundError: No module named 'HTMLParser'

Python语言学习:解决python版本升级问题集合(python2系列→Python3系列)导致错误的总结集合相关推荐

  1. Python语言学习:python编程之pip命令集合、python调式、头部代码、代码运行等常见概念详细攻略(解决问题为导向)

    Python语言学习:python编程之pip命令集合.python调式.头部代码.代码运行等常见概念详细攻略(解决问题为导向) 目录 一.pip命令集合 1.pip常规命令 1.1  pip下载se ...

  2. Python语言学习:Python语言学习之正则表达式常用函数之re.search方法【输出仅一个匹配结果(内容+位置)】、re.findall方法【输出所有匹配结果(内容)】案例集合之详细攻略

    Python语言学习:Python语言学习之正则表达式常用函数之re.search方法[输出仅一个匹配结果(内容+位置)].re.findall方法[输出所有匹配结果(内容)]案例集合之详细攻略 导读 ...

  3. Python语言学习:Python常用自带库(imageio、pickle)简介、使用方法之详细攻略

    Python语言学习:Python常用自带库(imageio.pickle)简介.使用方法之详细攻略 目录 imageio简介及其常见使用方法 pickle简介及其常见使用方法 简介 使用方法 简介及 ...

  4. Python语言学习:python语言代码调试—异常处理之详细攻略

    Python语言学习:python语言代码调试-异常处理之详细攻略 目录 python语言代码调试-异常处理 异常捕捉可以使用 try/except 语句 相关文章 Python3 错误和异常 | 菜 ...

  5. python语言学习:python语言学习中的定义类、定义函数、封装api等详细攻略

    python语言学习:python语言学习中的定义类.定义函数.封装api等详细攻略 目录 python语言学习中的定义类 python语言学习中的定义函数 python语言学习中封装api pyth ...

  6. Python语言学习:python语言的特点、入门、基础用法之详细攻略

    Python语言学习:python语言的特点.入门.基础用法之详细攻略 相关内容 Python 基础教程 目录 python语言的特点 python语言的入门 python语言的基础用法 python ...

  7. Python语言学习:Python语言学习之硬件交互应用(arduino、树莓派等)相关的简介、案例应用之详细攻略

    Python语言学习:Python语言学习之硬件交互应用(arduino.树莓派等)相关的简介.案例应用之详细攻略 目录 Python与硬件交互应用 1.适合运行python的嵌入式硬件系统 1.1. ...

  8. Python语言学习:Python随机生成那些事之随机生成使用方法、案例应用之详细攻略

    Python语言学习:利用Python随机生成那些事之随机生成使用方法.案例应用之详细攻略 目录 案例应用 1.随机生成指定区间内的某一值 案例应用 1.随机生成指定区间内的某一值 使用说明:给定区域 ...

  9. Python语言学习:利用pandas对两列字段元素求差集(对比两列字段所有元素的异同)

    Python语言学习:利用pandas对两列字段元素求差集(对比两列字段所有元素的异同) 目录 利用pandas对两列字段元素求差集(对比两列字段所有元素的异同) 输出结果 实现代码 利用pandas ...

最新文章

  1. linux中ctrl+z和ctrl+c的区别
  2. python set 排序_python set 排序_如何在Python中使用sorted()和sort()
  3. Python 技术篇 - python3使用speech库常见问题原因及解决方法
  4. 使用下列 else-if 排列来处理多个条件:
  5. 数码管时钟程序C语言00到99,[单片机]ACT89C51数码管时钟程序
  6. 2013年 833c语言程序 江南大学 (A卷)
  7. 深度学习(七十二)tensorflow 集群训练
  8. 游戏开发之nullptr和的NULL的区别(C++基础)
  9. Codeforces Beta Round #14 (Div. 2) D. Two Paths 树的直径
  10. dbv oracle驱动,Oracle DBV工具
  11. 010 Editor 8.0.1 之 逆向分析及注册机编写
  12. 玩转流量,天下无锅——IT运维人员的九阳神功(上)| 技术分享
  13. 走进小作坊(八)----公益之痒
  14. 大数据这么火,具体用用到哪些领域?揭秘大数据十三大具体应用场景
  15. ESP8266制作天气预报海藻球微景观生态缸记录(一)
  16. 普通路由器改4g路由器_4G工业路由器物联卡批发价格是多少?良心厂家推荐
  17. 易观千帆 | 2022年11月银行APP月活跃用户规模盘点
  18. 【BLE】CC2541之SBL
  19. Druid+Sqlite-JDBC+Kotlin,封装的一个都是毛病的工具类,奆佬们,评论区教一下我怎么封装
  20. 搭建Iconic+angular混合APP开发环境

热门文章

  1. ARM uboot Legacy uImage 和 fit img (Flattened uImage Tree)原理介
  2. WireShark过滤器选项
  3. centos6 rsync+inotify 数据同步
  4. php sort 不同类型导致的问题
  5. pycharm报错(Non-zero exit code (2))与手动安装报错
  6. vue post请求后台django接口Forbidden (CSRF token missing or incorrect.)
  7. 朋友,别告诉我你懂分布式事务!
  8. 低效能人士的七个习惯
  9. 谈谈To B业务的难点
  10. 再谈 HBase 八大应用场景