python 使用c模塊
by Adam Goldschmidt
亞當·戈德施密特(Adam Goldschmidt)
您可能沒有使用(但應該使用)的很棒的Python模塊 (Awesome Python modules you probably aren’t using (but should be))
Python is a beautiful language, and it contains many built-in modules that aim to help us write better, prettier code.
Python是一種漂亮的語言,它包含許多內置模塊,旨在幫助我們編寫更好,更漂亮的代碼。
目的 (Objective)
Throughout this article, we will use some lesser-known modules and methods that I think can improve the way we code - both in visibility and in efficiency.
在本文中,我們將使用一些鮮為人知的模塊和方法,我認為它們可以改善我們的編碼方式-可見性和效率。
命名元組 (NamedTuple)
I believe that some of you already know the more popular namedtuple
from the collections
module (if you don't - check it out), but since Python 3.6, a new class is available in the typing
module: NamedTuple
. Both are designed to help you quickly create readable immutable objects.
我相信有些人已經從collections
模塊中了解到了更流行的namedtuple
(如果您不這樣做的話,請檢查一下 ),但是自Python 3.6起, typing
模塊中提供了一個新類: NamedTuple
。 兩者都旨在幫助您快速創建可讀的不可變對象。
NamedTuple
is actually a typed version of namedtuple
, and in my opinion, is much more readable:
NamedTuple
實際上是一個類型版本namedtuple
,在我看來,是更可讀:
Here’s the namedtuple
alternative:
這是namedtuple
替代方案:
array.array (array.array)
Efficient arrays of numeric values. Arrays are sequence types and behave very much like lists, except that the type of objects stored in them is constrained. — Python docs
高效的數值數組。 數組是序列類型,其行為與列表非常相似,不同之處在于數組中存儲的對象類型受到約束。 — Python文檔
When using the array
module, we need to instantiate it with a typecode, which is the type all of its elements will use. Let's compare time efficiency with a normal list, writing many integers to a file (using pickle
module for a regular list):
使用array
模塊時,我們需要使用類型碼實例化它,這是其所有元素將使用的類型。 讓我們將時間效率與普通列表進行比較,向文件中寫入許多整數(對常規列表使用pickle
模塊):
14 times faster. That’s a lot. Of course it also depends on the pickle
module, but still - the array is way more compact than the list. So if you are using simple numeric values, you should consider using the array
module.
快14倍 。 好多啊。 當然,它也取決于pickle
模塊,但是仍然-數組比列表更緊湊。 因此,如果使用簡單的數值,則應考慮使用array
模塊。
itertools.combinations (itertools.combinations)
itertools
is an impressive module. It has so many different time-saving methods, all of them are listed here. There's even a GitHub repository containing more itertools!
itertools
是一個令人印象深刻的模塊。 它有很多不同的省時方法,所有這些都在這里列出。 甚至還有一個包含更多itertools的GitHub存儲庫!
I got to use the combinations
method this week and I thought I'd share it. This method takes an iterable and an integer as arguments, and creates a generator consisting of all possible combinations of the iterable with a maximum length of the integer given, without duplication:
我本周必須使用combinations
方法,我想我會分享的。 此方法將一個iterable和一個整數作為參數,并創建一個生成器,該生成器由iterable的所有可能組合以及給定整數的最大長度組成,且不重復:
dict.fromkeys (dict.fromkeys)
A quick and beautiful way of creating a dict with default values:
使用默認值創建字典的快速美觀的方法:
最后但并非最不重要的dis
模塊 (Last but not least - the dis
module)
The
dis
module supports the analysis of CPython bytecode by disassembling it.
dis
模塊通過反匯編來支持CPython 字節碼的分析。
As you may or may not know, Python compiles source code to a set of instructions called “bytecode”. The dis
module helps us handle these instructions, and it's a great debugging tool.
您可能知道也可能不知道,Python將源代碼編譯為一組稱為“字節碼”的指令。 dis
模塊可幫助我們處理這些指令,它是一個出色的調試工具。
Here’s an example from the Fluent Python book:
這是Fluent Python書中的一個示例:
We got an error — but the operation still succeeded. How come? Well, if we look at the bytecode (I added comments near the important parts):
我們遇到了錯誤-但操作仍然成功。 怎么會? 好吧,如果我們看一下字節碼(我在重要部分附近添加了注釋):
你走之前… (Before you go…)
Thanks for reading! For more Python related articles and other cool stuff, you can follow me on Medium or GitHub (I star some awesome repos!).
謝謝閱讀! 有關更多與Python相關的文章和其他有趣的內容,您可以在Medium或GitHub上關注我(我給一些超贊的存儲庫加注!)。
If you enjoyed this article, please hold down the clap button ? to help others find it. The longer you hold it, the more claps you give!
如果您喜歡這篇文章,請按住拍手按鈕? 幫助別人找到它。 握的時間越長,拍手就越多!
And do not hesitate to share more Python hidden gems in the comments below.
并且不要猶豫在下面的評論中分享更多Python隱藏的寶石。
翻譯自: https://www.freecodecamp.org/news/awesome-python-modules-you-probably-arent-using-but-should-be-ec926da27439/
python 使用c模塊