Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Peter Otten <__peter__@web.de> Newsgroups: comp.lang.python Subject: Re: Is vars() the most useless Python built-in ever? Date: Wed, 02 Dec 2015 08:51:56 +0100 Organization: None Lines: 25 Message-ID: References: <565cf141$0$1612$c3e8da3$5496439d@news.astraweb.com> <565e459b$0$1615$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Trace: news.uni-berlin.de p8PeG6I3VI36hF4qYALgmgeXnH3fZ0WclZPI46//GoYg== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'args': 0.04; 'subject:Python': 0.05; 'err:': 0.09; 'func': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.13; 'input.': 0.16; 'main():': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:ever': 0.16; 'subject:most': 0.16; 'wrote:': 0.16; 'try:': 0.18; 'programmer': 0.18; 'arguments': 0.22; 'parse': 0.22; 'programming': 0.22; 'script': 0.25; 'header:User-Agent:1': 0.26; 'header:X-Complaints- To:1': 0.26; 'error': 0.27; 'function': 0.28; 'code': 0.30; "d'aprano": 0.33; 'steven': 0.33; 'traceback': 0.33; 'except': 0.34; 'skip:p 30': 0.35; 'should': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'whatever': 0.39; 'subject:the': 0.39; 'subject:-': 0.39; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'your': 0.60; 'better.': 0.66; 'break.': 0.84; 'safer': 0.91 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: p57bd9e58.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:99847 Steven D'Aprano wrote: > and your code will break. Better and safer is: > > > def main(): # parse the args and call whatever function was selected > args = parser.parse_args(sys.argv[1:]) > try: > func = args.func > except AttributeError as err: > parser.print_help() > else: > func(vars(args)) I don't think this is any better. You are reponding on a programming error in the script as if the user had provided erroneous input. Very confusing. Just args.func(vars(args)) will produce a traceback if the programmer made an error and a help message if the user provides arguments that are not recognized. That's how it should be.