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: 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 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: 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: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 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