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

Jussi Piitulainen jpiitula at ling.helsinki.fi
Mon Jan 26 11:29:19 EST 2015


Ian Kelly writes:
> On Mon, Jan 26, 2015 at 8:50 AM, Adam Funk 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.

I've used tuples to convey the intention of immutability, as opposed
to using lists. For all I know, collecting a generator (from groupby)
into a tuple might be slower than collecting it into a list. I have no
intention to measure this. The programs have been fast enough for me.



More information about the Python-list mailing list