Why does argparse return None instead of [] if an append action isn't used?

Adam Funk a24061 at ducksburg.com
Mon Jan 26 16:50:21 CET 2015

On 2015-01-26, Peter Otten wrote:

> Adam Funk wrote:
>> On 2015-01-09, Ned Batchelder wrote:
>>> On 1/9/15 9:44 AM, Adam Funk wrote:
>>>> This makes it a bit more trouble to use:
>>>>    if options.bar:
>>>>       for b in options:bar
>>>>          do_stuff(b)
>>>> instead of
>>>>    for b in options.bar
>>>>       do_stuff(b)
>>> This doesn't answer why the value defaults to None, and some people may
>>> recoil at it, but I've used:
>>>      for b in options.bar or ():
>>>          do_stuff(b)
>> Do you mean "for b in options.bar or []:" ?
> Doesn't matter; in the context of a for loop any empty iterable would do.

Of course it would.  Doh!

A recent study conducted by Harvard University found that the average
American walks about 900 miles a year. Another study by the AMA found
that Americans drink, on average, 22 gallons of alcohol a year. This
means, on average, Americans get about 41 miles to the gallon.

More information about the Python-list mailing list