mirror of
https://github.com/LCTT/TranslateProject.git
synced 2024-12-26 21:30:55 +08:00
translated
This commit is contained in:
parent
85c7d6ca12
commit
086bdc4a39
@ -1,86 +0,0 @@
|
||||
[#]: subject: (3 features released in Python 3.1 you should use in 2021)
|
||||
[#]: via: (https://opensource.com/article/21/5/python-31-features)
|
||||
[#]: author: (Moshe Zadka https://opensource.com/users/moshez)
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (geekpi)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
|
||||
3 features released in Python 3.1 you should use in 2021
|
||||
======
|
||||
Explore some of the underutilized but still useful Python features.
|
||||
![Python programming language logo with question marks][1]
|
||||
|
||||
This is the second in a series of articles about features that first appeared in a version of Python 3.x. Python 3.1 was first released in 2009, and even though it has been out for a long time, many of the features it introduced are underused and pretty cool. Here are three of them.
|
||||
|
||||
### Thousands formatting
|
||||
|
||||
When formatting large numbers, it is common to place commas every three digits to make the number more readable (e.g., 1,048,576 is easier to read than 1048576). Since Python 3.1, this can be done directly when using string formatting functions:
|
||||
|
||||
|
||||
```
|
||||
`"2 to the 20th power is {:,d}".format(2**20)`[/code] [code]`'2 to the 20th power is 1,048,576'`
|
||||
```
|
||||
|
||||
The `,d` format specifier indicates that the number must be formatted with commas.
|
||||
|
||||
### Counter class
|
||||
|
||||
The `collections.Counter` class, part of the standard library module `collections`, is a secret super-weapon in Python. It is often first encountered in simple solutions to interview questions in Python, but its value is not limited to that.
|
||||
|
||||
For example, find the five most common letters in the first eight lines of [Humpty Dumpty's song][2]:
|
||||
|
||||
|
||||
```
|
||||
hd_song = """
|
||||
In winter, when the fields are white,
|
||||
I sing this song for your delight.
|
||||
|
||||
In Spring, when woods are getting green,
|
||||
I'll try and tell you what I mean.
|
||||
|
||||
In Summer, when the days are long,
|
||||
Perhaps you'll understand the song.
|
||||
|
||||
In Autumn, when the leaves are brown,
|
||||
Take pen and ink, and write it down.
|
||||
"""
|
||||
|
||||
```
|
||||
```
|
||||
|
||||
import collections
|
||||
|
||||
collections.Counter(hd_song.lower().replace(' ', '')).most_common(5)
|
||||
|
||||
```
|
||||
```
|
||||
`[('e', 29), ('n', 27), ('i', 18), ('t', 18), ('r', 15)]`
|
||||
```
|
||||
|
||||
### Executing packages
|
||||
|
||||
Python allows the `-m` flag to execute modules from the command line. Even some standard-library modules do something useful when they're executed; for example, `python -m cgi` is a CGI script that debugs the web server's CGI configuration.
|
||||
|
||||
However, until Python 3.1, it was impossible to execute _packages_ like this. Starting with Python 3.1, `python -m package` will execute the `__main__` module in the package. This is a good place to put debug scripts or commands that are executed mostly with tools and do not need to be short.
|
||||
|
||||
Python 3.0 was released over 11 years ago, but some of the features that first showed up in this release are cool—and underused. Add them to your toolkit if you haven't already.
|
||||
|
||||
Newcomers to python-ideas occasionally make reference to the idea of "Python 4000" when proposing...
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/21/5/python-31-features
|
||||
|
||||
作者:[Moshe Zadka][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://opensource.com/users/moshez
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/python_programming_question.png?itok=cOeJW-8r (Python programming language logo with question marks)
|
||||
[2]: http://www2.open.ac.uk/openlearn/poetryprescription/humpty-dumptys-recitation.html
|
@ -0,0 +1,84 @@
|
||||
[#]: subject: (3 features released in Python 3.1 you should use in 2021)
|
||||
[#]: via: (https://opensource.com/article/21/5/python-31-features)
|
||||
[#]: author: (Moshe Zadka https://opensource.com/users/moshez)
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (geekpi)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
|
||||
你应该在 2021 年使用的 Python 3.1 中发布的 3 个功能
|
||||
======
|
||||
探索一些未被充分利用但仍然有用的 Python 特性。
|
||||
![Python programming language logo with question marks][1]
|
||||
|
||||
这是关于首次出现在 Python 3.x 版本中的特性的系列文章的第二篇。Python 3.1 于 2009 年首次发布,尽管它已经发布了很长时间,但它引入的许多特性都没有被充分利用,而且相当酷。下面是其中的三个。
|
||||
|
||||
### 千位数格式化
|
||||
|
||||
在格式化大数时,通常是每三位数放置逗号,使数字更易读 (例如,1,048,576 比 1048576 更容易读)。从 Python 3.1 开始,可以在使用字符串格式化函数时直接完成:
|
||||
|
||||
|
||||
```
|
||||
`"2 to the 20th power is {:,d}".format(2**20)`[/code] [code]`'2 to the 20th power is 1,048,576'`
|
||||
```
|
||||
|
||||
`,d` 格式符表示数字必须用逗号格式化。
|
||||
|
||||
### Counter 类
|
||||
|
||||
`collections.Counter` 类是标准库模块 `collections` 的一部分,是 Python 中的一个秘密超级武器。它经常在 Python 的面试题的简单解答中首次遇到,但它的价值并不限于此。
|
||||
|
||||
例如,在 [Humpty Dumpty 的歌][2]的前八行中找出五个最常见的字母:
|
||||
|
||||
|
||||
```
|
||||
hd_song = """
|
||||
In winter, when the fields are white,
|
||||
I sing this song for your delight.
|
||||
|
||||
In Spring, when woods are getting green,
|
||||
I'll try and tell you what I mean.
|
||||
|
||||
In Summer, when the days are long,
|
||||
Perhaps you'll understand the song.
|
||||
|
||||
In Autumn, when the leaves are brown,
|
||||
Take pen and ink, and write it down.
|
||||
"""
|
||||
|
||||
```
|
||||
```
|
||||
|
||||
import collections
|
||||
|
||||
collections.Counter(hd_song.lower().replace(' ', '')).most_common(5)
|
||||
|
||||
```
|
||||
```
|
||||
`[('e', 29), ('n', 27), ('i', 18), ('t', 18), ('r', 15)]`
|
||||
```
|
||||
|
||||
### 执行软件包
|
||||
|
||||
Python 允许使用 `-m` 标志来从命令行执行模块。甚至一些标准库模块在被执行时也会做一些有用的事情;例如,`python -m cgi` 是一个 CGI 脚本,用来调试网络服务器的 CGI 配置。
|
||||
|
||||
然而,直到 Python 3.1,都不可能像这样执行_软件包_。从 Python 3.1 开始,`python -m package` 将执行软件包中的 `__main__` 模块。这是一个放调试脚本或命令的好地方,这些脚本主要是用工具执行的,不需要很短。
|
||||
|
||||
Python 3.0 在 11 年前就已经发布了,但是在这个版本中首次出现的一些功能是很酷的,而且没有得到充分利用。如果你还没使用,那么将它们添加到你的工具箱中。
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/21/5/python-31-features
|
||||
|
||||
作者:[Moshe Zadka][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[geekpi](https://github.com/geekpi)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://opensource.com/users/moshez
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/python_programming_question.png?itok=cOeJW-8r (Python programming language logo with question marks)
|
||||
[2]: http://www2.open.ac.uk/openlearn/poetryprescription/humpty-dumptys-recitation.html
|
Loading…
Reference in New Issue
Block a user