"Better option" from the python library, any list?
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
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
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.
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.
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!
精彩评论