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

Ian Kelly ian.g.kelly at gmail.com
Mon Jan 26 11:13:38 EST 2015


On Mon, Jan 26, 2015 at 8:50 AM, Adam Funk <a24061 at ducksburg.com> wrote:
> On 2015-01-26, Peter Otten wrote:
>
>> Adam Funk wrote:
>>
>>> On 2015-01-09, Ned Batchelder wrote:
>>>>      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!

Stylistically, I generally prefer the empty list for this. The empty
tuple might be a little faster since it's a singleton and doesn't need
to be constructed at runtime, but that's clearly a micro-optimization,
and I think the list more accurately conveys the intention of
"something to be iterated over". Although tuples are iterable, I don't
often use them for that purpose.



More information about the Python-list mailing list