I noticed in use that if an option with the 'append' action isn't
used, argparse assigns None to it rather than an empty list, &
confirmed this interactively:

#v+
>>> import argparse
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('--foo', action='append')
_AppendAction(option_strings=['--foo'], dest='foo', nargs=None, const=None, 
default=None, type=None, choices=None, help=None, metavar=None)
>>> parser.add_argument('--bar', action='append')
_AppendAction(option_strings=['--bar'], dest='bar', nargs=None, const=None, 
default=None, type=None, choices=None, help=None, metavar=None)
>>> parser.parse_args('--foo 1 --foo 2'.split())
Namespace(bar=None, foo=['1', '2'])
#v-

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)

which is (of course) what I was doing when I discovered the None.  Is
there any benefit to the user from this, or is it just an "accident"
of the way argparse is written?


-- 
The history of the world is the history of a privileged few.
                                            --- Henry Miller
-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to