“更好的选择”来自 python 库,有列表吗?

发布于 2024-08-13 15:41:36 字数 359 浏览 8 评论 0原文

我刚刚发现 optparse 模块的存在。我个人总是使用 getopt,所以我不想寻找更好的东西。然而,很明显 optparse 更加先进,因此我希望它成为将来从命令行获取选项的首选方式。

不管怎样,这个事件让我印象深刻。我现在想知道我从一开始就使用的模块或函数是否在标准库中有更好的替代品。是否有这样一个紧凑且快速浏览的列表,类似于“以前的解决方案:getopt。更好的解决方案:optparse(自 python 2.x 起)”?

按照约定编辑标记为 CW。

  • 解析命令行选项:getopt、optparse、argparse
  • 包管理:distutils、setuptools

I just found out the existence of the optparse module. I personally always used getopt, so I did not care to look for something better. It's clear, however, that optparse is much more advanced, so I would expect it to be the preferred way in the future to get options from the command line.

Anyway, this event struck me. I am now wondering if there are modules or functions out there I am using since the beginning of time, that have much better alternatives in the standard library. Is there such a compact and quick to browse list, on the liking of "previous solutions: getopt. better solution: optparse (since python 2.x)" ?

Edit marked as CW as agreed.

  • parsing command line options: getopt, optparse, argparse
  • package management: distutils, setuptools

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

暖树树初阳… 2024-08-20 15:41:36

我建议这可能是开始这样一个列表的好地方,

请注意,有 pep389 可以用 argparse 替换 optparse

collections.defaultdict 在大多数您会使用 dict.setdefault

集合模块是一个值得熟悉的好模块,因为它在 Python3 中有很多新东西

如果您不需要保留列表,则生成器表达式通常比列表推导式更好

三元运算符 b if a else c< /code> 而不是 a 和 b 或 c 及其所有问题

multiprocessing 替换您执行此操作的任何其他方式;)

itertools.izip_longest 避免当你压缩不平坦的东西时必须使用解决方法

I suggest this might be a good place to start such a list

note that there is pep389 to replace optparse with argparse

collections.defaultdict works nicer in most places you would use dict.setdefault

the collections module is a good one to become familiar with as it has lots of new stuff in Python3

Generator expressions are often better than list comprehensions if you don't need to keep the list

Ternary operator b if a else c instead of a and b or c with all it's problems

multiprocessing replaces any other way you were doing it ;)

itertools.izip_longest avoids having to use workarounds when you are zipping uneven things

三五鸿雁 2024-08-20 15:41:36

不完全紧凑,仅指标准库(以及标准 Python 的其他部分),而不指任何第三方包,所有 “Python XX 有什么新功能?” 文章。

除此之外,还有谷歌,我认为除了随机博客等之外,没有任何这样的列表。

Not exactly compact, and referring only to the standard library (and other parts of standard Python) but not any third-party packages, there are all the "What's New in Python X.X?" essays.

Other than that, and Google, I don't think there are any such lists except in random blogs and so forth.

幸福%小乖 2024-08-20 15:41:36

我不会完全同意“optparsegetopt 更好”的说法。如果 optparse 更适合您,并不意味着有人不会觉得 getopt 更可取。它们有不同的用途:getopt 更简单,开始使用时需要较少的理解(特别是如果您熟悉其他来源的 getopt:例如 shell 脚本),optparse更强大,更详细。但是,如果我只需要获取一两个命令参数,我什至可以使用简单的 if 语句。

总而言之,每种工具都有其用途,每种情况都可能需要更适合该情况的不同工具。

I wouldn't absolutely agree with a statement "optparse is better than getopt". If optparse suites you better, it doesn't mean someone wouldn't find getopt much preferable. They are intended for different purposes: getopt is much simpler and requires less understanding to start using (especially if you are familiar with getopt from other sources: e.g. shell scripting), optparse is more powerful and is more detailed. However, if I need to just get one or two command lime parameters, I might even use simple if statement.

To summarize, every tool has its purpose, and every situation might require a different tool which is more suitable for that situation.

蒲公英的约定 2024-08-20 15:41:36

我使用 Richard Gruet 的 Python 快速参考,这是关于 Python 的所有内容的一个很好的参考包括标准库的一些更重要的部分。它使用颜色编码和注释很好地改变了流行的语言和库。

例如,请查看有关 getopt 的部分,以及基础发行版中的模块和包列表

它还没有针对 Python 3 进行更新,但我充满希望!

I use Richard Gruet's Python Quick Reference which is a great reference for all things Python including some of the more important parts of the standard library. It does a good job of making changes in the language and library prevalent using colour coding and notes.

Take a look at his section on getopt, for instance, and the list of modules and packages in base distribution.

It's not been updated for Python 3 yet, but I live in hope!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文