Path: csiph.com!news.swapon.de!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: Tue, 01 Dec 2015 11:40:15 +0100 Organization: None Lines: 79 Message-ID: References: <565cf141$0$1612$c3e8da3$5496439d@news.astraweb.com> <20151201084416.GA2700@beagle> Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Trace: news.uni-berlin.de 9/YxC2GmH+nxy/DCv+l2ggHSCcgtOSbrLp+wyZA3rLMw== 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; 'bits': 0.07; 'main()': 0.07; 'rewrite': 0.07; 'true)': 0.07; 'err:': 0.09; 'flush': 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; 'snippet': 0.09; 'url:github': 0.09; 'exception': 0.13; 'def': 0.13; '"all"': 0.16; 'main():': 0.16; 'podcast': 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; 'attribute': 0.18; 'beginner': 0.18; 'try:': 0.18; 'typical': 0.18; 'exceptions': 0.22; 'exists.': 0.22; 'function:': 0.22; 'keyerror:': 0.22; 'parse': 0.22; 'pass': 0.22; 'trying': 0.22; 'bit': 0.23; 'passing': 0.23; 'script': 0.25; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'function': 0.28; 'feeds': 0.29; 'thread,': 0.29; 'value)': 0.29; "i'm": 0.30; 'code': 0.30; 'probably': 0.31; 'anyone': 0.32; 'language.': 0.32; "d'aprano": 0.33; 'steven': 0.33; 'file': 0.34; 'except': 0.34; 'handle': 0.34; 'could': 0.35; 'feed': 0.35; 'newer': 0.35; 'skip:p 30': 0.35; 'but': 0.36; 'there': 0.36; 'skip:{ 10': 0.36; 'to:addr :python-list': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'wrong': 0.38; 'skip:p 20': 0.38; 'why': 0.39; 'whatever': 0.39; 'does': 0.39; 'subject:the': 0.39; 'subject:-': 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; 'received:de': 0.40; 'some': 0.40; 'your': 0.60; 'skip:u 10': 0.61; 'information': 0.63; 'judge': 0.66; 'oldest': 0.66; 'life': 0.67; 'answer:': 0.84 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: p57bd9ec4.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:99786 Manolo Martínez wrote: > On 12/01/15 at 12:00pm, Steven D'Aprano wrote: > > I'm trying to understand why vars() exists. Does anyone use it? > > Well, I have this little podcast aggregator > (https://github.com/manolomartinez/greg) that I and a bunch of other > people use. I started writing it some years ago, and the code is a bit > of a palimpsest, newer functionality reflecting my (somewhat) better > understanding of the language. One of the oldest bits is this main() > function: > > def main(): # parse the args and call whatever function was selected > try: > args = parser.parse_args(sys.argv[1:]) > args.func(vars(args)) > except AttributeError as err: > if str(err) == "\'Namespace\' object has no attribute \'func\'": > parser.print_help() > else: > print("Something has gone wrong: {}".format(err), file = sys.stderr, flush = True) > > > To judge by this thread, this is probably wrong/noobish? What probably is typical for a beginner in that snippet is that you don't trust the exception system and handle exceptions that will never occur once the script is debugged. Just write args = parser.parse_args() args.func(vars(args)) Now vars(). I see nothing wrong with it, but when I look into one of your func implementations > def info(args): # Provides information of a number of feeds > session = Session(args) > if "all" in args["names"]: > feeds = session.list_feeds() > else: > feeds = args["names"] > for feed in feeds: > pretty_print(session, feed) I come to the conclusion that passing args directly could make your life easier: def info(args): """Provides information of a number of feeds""" session = Session(args) if "all" in args.names: feeds = session.list_feeds() else: feeds = args.names for feed in feeds: pretty_print(session, feed) As far as I can see there is only one place where the key is not a constant, and you can rewrite that from > try: > if args[value]: > return args[value] > except KeyError: > pass to try: answer = getattr(args, value) if answer: return answer except AttributeError: pass