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


Groups > comp.lang.python > #83453

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

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!ecngs!feeder2.ecngs.de!217.188.199.168.MISMATCH!takemy.news.telefonica.de!telefonica.de!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'argument': 0.05; 'parser': 0.07; 'arguments': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:None': 0.09; 'subject:Why': 0.09; 'adam': 0.16; 'assigns': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'right:': 0.16; 'subject: \n ': 0.16; 'subject:argparse': 0.16; 'wrote:': 0.18; 'import': 0.22; 'header:User-Agent:1': 0.23; 'documented': 0.24; 'skip': 0.24; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; '>>>>': 0.31; 'received:132': 0.31; 'option': 0.32; 'says': 0.33; 'url:python': 0.33; 'call.': 0.33; 'used,': 0.33; 'noticed': 0.34; 'add': 0.35; 'subject:?': 0.36; 'url:org': 0.36; 'should': 0.36; 'confirmed': 0.38; 'url:library': 0.38; 'to:addr:python-list': 0.38; 'list,': 0.38; 'pm,': 0.38; 'rather': 0.38; 'skip:_ 30': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'url:3': 0.61; 'here:': 0.62; 'skip:n 10': 0.64; 'default': 0.69
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de>
Subject Re: Why does argparse return None instead of [] if an append action isn't used?
Date Fri, 09 Jan 2015 16:50:07 +0100
References <l265obxc6d.ln2@news.ducksburg.com>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host px1.ruf.uni-freiburg.de
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.3.0
In-Reply-To <l265obxc6d.ln2@news.ducksburg.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.17532.1420818709.18130.python-list@python.org> (permalink)
Lines 31
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1420818709 news.xs4all.nl 2977 [2001:888:2000:d::a6]:55395
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:83453

Show key headers only | View raw


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.

Wolfgang

Back to comp.lang.python | Previous | NextPrevious in thread | Next 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