日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

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

發布時間:2025/3/21 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python语言学习:解决python版本升级问题集合(python2系列→Python3系列)导致错误的总结集合 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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系列)导致错误的总结集合的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。