“更好的选择”来自 python 库,有列表吗?
我刚刚发现 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我建议这可能是开始这样一个列表的好地方,
请注意,有 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 usedict.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 ofa and b or c
with all it's problemsmultiprocessing
replaces any other way you were doing it ;)itertools.izip_longest
avoids having to use workarounds when you are zipping uneven things不完全紧凑,仅指标准库(以及标准 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.
我不会完全同意“
optparse
比getopt
更好”的说法。如果optparse
更适合您,并不意味着有人不会觉得getopt
更可取。它们有不同的用途:getopt
更简单,开始使用时需要较少的理解(特别是如果您熟悉其他来源的 getopt:例如 shell 脚本),optparse
更强大,更详细。但是,如果我只需要获取一两个命令参数,我什至可以使用简单的if
语句。总而言之,每种工具都有其用途,每种情况都可能需要更适合该情况的不同工具。
I wouldn't absolutely agree with a statement "
optparse
is better thangetopt
". Ifoptparse
suites you better, it doesn't mean someone wouldn't findgetopt
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 simpleif
statement.To summarize, every tool has its purpose, and every situation might require a different tool which is more suitable for that situation.
我使用 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!