Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #84599

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

From Adam Funk <a24061@ducksburg.com>
Newsgroups comp.lang.python
Subject Re: Why does argparse return None instead of [] if an append action isn't used?
Date 2015-01-26 13:43 +0000
Organization $CABAL
Message-ID <msshpbxjvv.ln2@news.ducksburg.com> (permalink)
References <l265obxc6d.ln2@news.ducksburg.com> <mailman.17532.1420818709.18130.python-list@python.org>

Show all headers | View raw


On 2015-01-09, Wolfgang Maier wrote:

> On 01/09/2015 03:44 PM, Adam Funk wrote:
>> 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-
>>
>
> Isn't that the exact behaviour documented here:
>
> https://docs.python.org/3/library/argparse.html#default
>
> where it says that the default for the default argument is None ?
>
> I think Skip is right: you should be able to just add
>
> default = []
>
> to your arguments in the add_argument call.

Yes, it works.


-- 
Master Foo said: "A man who mistakes secrets for knowledge is like
a man who, seeking light, hugs a candle so closely that he smothers
it and burns his hand."                            --- Eric Raymond

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Why does argparse return None instead of [] if an append action isn't used? Adam Funk <a24061@ducksburg.com> - 2015-01-09 14:44 +0000
  Re: Why does argparse return None instead of [] if an append action isn't used? Skip Montanaro <skip.montanaro@gmail.com> - 2015-01-09 08:57 -0600
    Re: Why does argparse return None instead of [] if an append action isn't used? Adam Funk <a24061@ducksburg.com> - 2015-01-09 15:25 +0000
  Re: Why does argparse return None instead of [] if an append action isn't used? Ned Batchelder <ned@nedbatchelder.com> - 2015-01-09 10:12 -0500
    Re: Why does argparse return None instead of [] if an append action isn't used? Adam Funk <a24061@ducksburg.com> - 2015-01-26 13:43 +0000
      Re: Why does argparse return None instead of [] if an append action isn't used? Peter Otten <__peter__@web.de> - 2015-01-26 15:04 +0100
        Re: Why does argparse return None instead of [] if an append action isn't used? Adam Funk <a24061@ducksburg.com> - 2015-01-26 15:50 +0000
          Re: Why does argparse return None instead of [] if an append action isn't used? Ian Kelly <ian.g.kelly@gmail.com> - 2015-01-26 09:13 -0700
            Re: Why does argparse return None instead of [] if an append action isn't used? Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2015-01-26 18:29 +0200
              Re: Why does argparse return None instead of [] if an append action isn't used? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-01-26 17:18 +0000
              Re: Why does argparse return None instead of [] if an append action isn't used? Ian Kelly <ian.g.kelly@gmail.com> - 2015-01-26 11:23 -0700
              Re: Why does argparse return None instead of [] if an append action isn't used? Ian Kelly <ian.g.kelly@gmail.com> - 2015-01-26 11:26 -0700
  Re: Why does argparse return None instead of [] if an append action isn't used? Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> - 2015-01-09 16:50 +0100
    Re: Why does argparse return None instead of [] if an append action isn't used? Adam Funk <a24061@ducksburg.com> - 2015-01-26 13:43 +0000

csiph-web