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.
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隐藏的宝石。
python 使用c模块