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

Ned Batchelder ned at nedbatchelder.com
Fri Jan 9 16:12:39 CET 2015


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)

-- 
Ned Batchelder, http://nedbatchelder.com




More information about the Python-list mailing list