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模块

python 使用c模块_您可能没有使用(但应该使用)的很棒的Python模块相关推荐

  1. 更适合python的应用程序_一些很棒的Python应用程序

    我们可以从180多个成功发布的Python软件案例中学到什么. 我很高兴地发布一些很棒的Python应用程序,这是一个手工整理的180多个项目的列表,所有这些都是: 1.带有在线源代码仓库的免费软件. ...

  2. python 创意项目_针对python开发人员的10个很棒的python项目创意

    python 创意项目 The joy of coding Python should be in seeing short, concise, readable classes that expre ...

  3. python需要的基础_推荐收藏!小白不要怕!一周学全Python面试基础(2)

    Python是一个广泛的领域,因此有必要保持最新状态.通过列出30个python面试问题和答案,本文涵盖在Python面试中经常问到的问题.如果您是该行业的新手,本基础篇将极大地帮助您.我们衷心希望这 ...

  4. python 与或非_“2020”假看完本文若不能让你学通“Python”,我将永远退出IT界!...

    0基础小白怎么学Python? 今天给大家分享一些学习Python的基本概念图,入门书籍.视频教程以及学习路上最有效的方法.Python基本概念最全图1.Python 解释器: 2.Python数据结 ...

  5. python 三维地球库_用于GIS(地理信息系统)和三维可视化制图的Python库

    前言 Python库是GIS中的终极扩展,因为它允许您增强其核心功能. 通过使用Python库,您可以摆脱GIS的束缚,而深入研究一些严肃的数据科学. Python中有200多个标准库.但是也有成千上 ...

  6. python图片转文字_【收藏】图片转成文字的方法总结,python批量图片转文字信息参考源码...

    在日常办公或者学习中,往往存在这样一个工作场景,比如,"老王,我这里有一张图片,你把里面的文字信息给我整理出来",都2021年了,你真的还在手敲图片文字信息么?那么还不赶紧收藏这篇 ...

  7. java与python多态的区别_如果未调用父构造函数(与Java不同),多态性在Python中如何工作? - java...

    因此,父类构造函数是在Java中调用的,而在Python中则不是.如果这意味着未创建父对象,那么如何在Python中成功调用def function-这是怎么回事? Python代码 class Pa ...

  8. python 5的倍数_查找所有低于1000的数字的和,这是Python中3或5的倍数

    python 5的倍数 Sometimes, we need to find the sum of all integers or numbers that are completely divisi ...

  9. python降低图片分辨率_手把手:扫描图片又大又不清晰?这个Python小程序帮你搞定!...

    原标题:手把手:扫描图片又大又不清晰?这个Python小程序帮你搞定! 大数据文摘作品 编译:HAPPEN.于乐源.小鱼 一位乐于分享学生精彩笔记的大学教授对于扫描版的文件非常不满意--颜色不清晰并且 ...

最新文章

  1. 应用中有使用到集群么?多大规模?
  2. 异步任务下载apk文件并弹出对话框提示当前进度,文件下载结束后弹出安装界面
  3. 解决:Unable to identify index name. XXXModel is not a Document. Make sure the document class is annota
  4. 景区门票系统上云 低成本、安全性高
  5. border-collapse 关于继承问题
  6. linux g++ undefined reference to `dlopen'类问题
  7. [误将两块磁盘挂载到根目录][解决办法]
  8. The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local Machine 完美解决
  9. AddThis AddFeed WordPress插件发布 [转]
  10. win7怎么像xp一样搜索计算机,win7可以象XP那样直接在我的电脑里面直接打开摄像头吗...
  11. 如何使用计算机求和公式,excel求和公式怎么用
  12. 仿真树叶飘落效果的实现
  13. 超大水箱持久水润,让这个冬天不干燥,摩飞有雾加湿器体验
  14. 随興8作者雨落下無痕
  15. 用ros输出hello,world(c++版)
  16. 2021年最后一期 | 转录组分析的正确姿势你了解了吗?
  17. 2022年外贸公司邮箱签名怎么设置?
  18. ERP咨询顾问必备的7种公关能力
  19. MATLAB----Roberts锐化滤波器
  20. poi 替换word里的\n 变成 换行

热门文章

  1. 【信号】函数kill、raise、abort、alarm
  2. sigsuspend函数(mysleep函数的改进)
  3. 资深Android开发带你入门Framework,再不刷题就晚了!
  4. 《HTTP 权威指南》笔记:第十五章 实体与编码
  5. KindEditor解决上传视频不能在手机端显示的问题
  6. C#中实现对象的深拷贝
  7. 安卓中经常使用控件遇到问题解决方法(持续更新和发现篇幅)(在textview上加一条线、待续)...
  8. java操作mongodb(连接池)(转)
  9. BZOJ 1270: [BeijingWc2008]雷涛的小猫( dp )
  10. C# winform 自定义皮肤制作