[Python-Dev] argparse ugliness

Nick Coghlan ncoghlan at gmail.com
Sat Mar 6 13:50:47 CET 2010


Neal Becker wrote:
> I generally enjoy argparse, but one thing I find rather
> ugly and unpythonic.
> 
>     parser.add_argument ('--plot', action='store_true')
> 
> Specifying the argument 'action' as a string is IMO ugly.

It isn't ideal, but having to grab constants from the module isn't ideal
either.

Using a string here allows argparse (and optparse before it) to spit out
an explicit "unknown action" message. Using named constants a typo would
just get you a generic NameError instead. With variable names only being
checked at runtime, you wouldn't get the static name checking that is
the primary benefit of using named constants in less dynamic languages.

So, agreed that using strings isn't pretty, but there aren't any great
alternatives available.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------


More information about the Python-Dev mailing list