Enchancement suggestion for argparse: intuit type from default

MRAB python at mrabarnett.plus.com
Thu Mar 15 03:10:11 CET 2012


On 15/03/2012 00:52, Roy Smith wrote:
> In article<4f612a9d$0$12033$742ec2ed at news.sonic.net>,
>   John Nagle<nagle at animats.com>  wrote:
>
>>  On 3/13/2012 2:08 PM, Roy Smith wrote:
>>  >  Using argparse, if I write:
>>  >
>>  >       parser.add_argument('--foo', default=100)
>>  >
>>  >  it seems like it should be able to intuit that the type of foo should
>>  >  be int (i.e. type(default)) without my having to write:
>>  >
>>  >       parser.add_argument('--foo', type=int, default=100)
>>  >
>>  >  Does this seem like a reasonable enhancement to argparse?
>>
>>      default=None
>>
>>  presents some problems.
>
> I'll admit I hadn't considered that, but I don't see it as a major
> problem.  The type intuition could be designed to only work for types
> other than NoneType.

True, you could consider that a special case.

If you really do want NoneType, or if the type doesn't otherwise match
the default (or there's no default), then you can still be explicit.



More information about the Python-list mailing list