124 lines
4.6 KiB
Markdown
124 lines
4.6 KiB
Markdown
# Python 3 的优势
|
||
|
||
> 原文:[https://python . land/migrating-from-python-2-to-3/python-3-advantages](https://python.land/migrating-from-python-2-to-3/python-3-advantages)
|
||
|
||
让我们探索一下 Python 3 相对于 Python 2 的优势吧!
|
||
|
||
目录
|
||
|
||
|
||
|
||
* [Print 不再是一个语句,而是一个内置函数](#Print_is_no_longer_a_statement_but_a_built-in_function "Print is no longer a statement but a built-in function")
|
||
* [Python 3 中的 Unicode](#Unicode_in_Python_3 "Unicode in Python 3")
|
||
* [数据类别](#Data_classes "Data classes")
|
||
* [合并字典(Python 3.5+)](#Merging_dictionaries_Python_35 "Merging dictionaries (Python 3.5+)")
|
||
* [分歧变得更加可预测](#Divisions_became_more_predictable "Divisions became more predictable")
|
||
* [有意义的比较](#Meaningful_comparisons "Meaningful comparisons")
|
||
* [无更多范围对比 x 范围](#No_more_range_vs_xrange "No more range vs. xrange")
|
||
|
||
|
||
|
||
## Print 不再是一个语句,而是一个内置函数
|
||
|
||
在 Python 3 中,print 变成了一个[函数调用](https://python.land/introduction-to-python/functions)而不是一个语句。Python 3 中这一变化的一些优点是:
|
||
|
||
* 印刷品真的没有理由成为一种陈述。如果打印是一种功能,那就更符合了。
|
||
* 因为 print 是一个函数,所以它可以作为参数传递给需要函数的函数。例如,将需要另一个函数进一步处理数据的函数作为参数。对于简单的模拟/调试,您现在还可以传递 print()函数。
|
||
* 你现在可以像这样使用 print,因为它是一个函数:`[print(x) for x in range(10)]`
|
||
* 您可以通过赋值给`builtins.print`来覆盖打印功能,但是您不能用语句来这样做。
|
||
|
||
## Python 3 中的 Unicode
|
||
|
||
Python 3 的另一大优势是,默认情况下,每个字符串都是 Unicode 字符串。在 Python 2 中,字符串默认为 ASCII 字符串,这限制了它可以处理的字符范围。如果您想要一个 Unicode 字符串,您必须显式地创建一个这样的字符串:
|
||
|
||
```py
|
||
# Python 2
|
||
unicode_string = u'Ümlaut? Nō prōblem!'
|
||
|
||
# Python 3
|
||
unicode_string = 'Ümlaut? Nō prōblem!'
|
||
```
|
||
|
||
这是很多国家必备的。
|
||
|
||
## 数据类别
|
||
|
||
从最近的 3.7 版本开始, [Python 提供了数据类](https://python.land/python-data-classes)。与常规类或其他替代方法相比,它有几个优点,比如返回多个值或字典:
|
||
|
||
* 数据类需要最少的代码。
|
||
* 你可以比较数据类,因为`__eq__`是为你实现的。
|
||
* 您可以轻松地打印一个用于调试的数据类,因为也实现了`__repr__`。
|
||
* 数据类需要类型提示,减少了出错的机会。
|
||
|
||
Thank you for reading my tutorials. I write these in my free time, and it requires a lot of time and effort. I use ads to keep writing these *free* articles, I hope you understand! **Support me by disabling your adblocker on my website** or, alternatively, **[buy me some coffee](https://www.buymeacoffee.com/pythonland)**. It's much appreciated and allows me to keep working on this site!
|
||
|
||
下面是一个数据类工作的例子:
|
||
|
||
```py
|
||
from dataclasses import dataclass
|
||
|
||
@dataclass
|
||
class Card:
|
||
rank: str
|
||
suit: str
|
||
|
||
card = Card("Q", "hearts")
|
||
|
||
print(card == card)
|
||
# True
|
||
|
||
print(card.rank)
|
||
# 'Q'
|
||
|
||
print(card)
|
||
Card(rank='Q', suit='hearts')
|
||
```
|
||
|
||
## 合并字典(Python 3.5+)
|
||
|
||
从 Python 3.5 开始,合并[字典](https://python.land/python-datatypes/dictionaries)变得更加容易:
|
||
|
||
```py
|
||
dict1 = { 'a': 1, 'b': 2 }
|
||
dict2 = { 'b': 3, 'c': 4 }
|
||
merged = { **dict1, **dict2 }
|
||
print (merged)
|
||
# {'a': 1, 'b': 3, 'c': 4}
|
||
```
|
||
|
||
如果有重叠的关键字,第一个字典中的关键字将被覆盖。
|
||
|
||
## 分歧变得更加可预测
|
||
|
||
在 Python 2 中,除法运算符`/`默认为整数除法,除非其中一个操作数是浮点数。所以你有这样的行为:
|
||
|
||
```py
|
||
# Python 2
|
||
5 / 2 = 2
|
||
5 / 2.0 = 2.5
|
||
```
|
||
|
||
在 Python 3 中,除法运算符默认为浮点除法,而//运算符变成了整数除法。所以我们得到:
|
||
|
||
```py
|
||
# Python 3
|
||
5 / 2 = 2.5
|
||
5 // 2 = 2
|
||
```
|
||
|
||
对于这一变化背后的完整动机,你应该阅读 PEP-0238。
|
||
|
||
## 有意义的比较
|
||
|
||
在 Python 2 中,您可以将任何东西与任何东西进行比较。以下示例将全部返回`True`:
|
||
|
||
```py
|
||
"a string" > 2
|
||
None < 5
|
||
```
|
||
|
||
没有意义,还能藏 bug。在 Python 3 中,这些比较会抛出一个`TypeError` [异常](https://python.land/deep-dives/python-try-except)。
|
||
|
||
## 无更多范围对比 x 范围
|
||
|
||
Python 2 有两个范围函数:`range`和`xrange`。后者更快,因为它基于[迭代器](https://python.land/deep-dives/python-iterator)。在 Python 3 中,`range`变成了`xrange`,而`xrange`的名字被去掉了。这是 Python 变得不那么让新手困惑的例子之一。 |