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


淡淡与蛋挞
淡淡与蛋挞 2022-09-20 10:45:45 50621
分类专栏: 资讯

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入门技能树基础语法内置类123871 人正在系统学习中

网站声明:如果转载,请联系本站管理员。否则一切后果自行承担。

本文链接:https://www.xckfsq.com/news/show.html?id=4100
赞同 0
评论 0 条
淡淡与蛋挞L0
粉丝 0 发表 9 + 关注 私信
上周热门
如何使用 StarRocks 管理和优化数据湖中的数据?  2935
【软件正版化】软件正版化工作要点  2854
统信UOS试玩黑神话:悟空  2811
信刻光盘安全隔离与信息交换系统  2702
镜舟科技与中启乘数科技达成战略合作,共筑数据服务新生态  1235
grub引导程序无法找到指定设备和分区  1205
点击报名 | 京东2025校招进校行程预告  162
华为全联接大会2024丨软通动力分论坛精彩议程抢先看!  160
2024海洋能源产业融合发展论坛暨博览会同期活动-海洋能源与数字化智能化论坛成功举办  156
华为纯血鸿蒙正式版9月底见!但Mate 70的内情还得接着挖...  154
本周热议
我的信创开放社区兼职赚钱历程 40
今天你签到了吗? 27
信创开放社区邀请他人注册的具体步骤如下 15
如何玩转信创开放社区—从小白进阶到专家 15
方德桌面操作系统 14
我有15积分有什么用? 13
用抖音玩法闯信创开放社区——用平台宣传企业产品服务 13
如何让你先人一步获得悬赏问题信息?(创作者必看) 12
2024中国信创产业发展大会暨中国信息科技创新与应用博览会 9
中央国家机关政府采购中心:应当将CPU、操作系统符合安全可靠测评要求纳入采购需求 8

加入交流群

请使用微信扫一扫!