Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Ian Kelly Newsgroups: comp.lang.python Subject: Re: Try: rather than if : Date: Mon, 14 Dec 2015 16:53:49 -0700 Lines: 27 Message-ID: References: <20151214231420.GA20631@cskk.homeip.net> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de hmvsQdcJpEL+1cLV2jrwhwCQvxO+M6NyLgBN76CwIFkA== 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; 'else:': 0.03; '%s\\n"': 0.09; 'attribute.': 0.09; 'name)': 0.09; 'exception': 0.13; 'accesses': 0.16; 'attribute,': 0.16; 'better:': 0.16; 'emit': 0.16; 'fetches': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'simpson': 0.16; 'try/except': 0.16; 'wrote:': 0.16; 'attribute': 0.18; 'try:': 0.18; '2015': 0.20; 'first,': 0.20; 'issue.': 0.20; 'pass': 0.22; 'dec': 0.23; 'header:In-Reply-To:1': 0.24; 'mon,': 0.24; 'message-id:@mail.gmail.com': 0.27; '14,': 0.27; 'catching': 0.29; 'raise': 0.29; 'code': 0.30; 'probably': 0.31; 'possibly': 0.32; 'except': 0.34; 'handle': 0.34; 'received:google.com': 0.35; 'should': 0.36; 'received:209.85': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'received:209.85.213': 0.37; 'version': 0.38; 'received:209': 0.38; 'to:addr:python.org': 0.40; 'your': 0.60; 'cameron': 0.66; 'intent': 0.66; 'to:name:python': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=EowyOR83n/UC8O6VpwLeaQUDicDCI5NM7KsqPZDABjg=; b=PBNmYg6eNoBPt9/d/cw6P+tTIxhsP8olLr8EZ+6DgET7juxHb1jjxBdF9+SLEID7Mx 4zJXnXvbThsSfIJ+1ZGwJ2nw/bBPpAxK/3NM+sD/mlK5r4Ds8xxKlr0P7k/lZL0vT+RD HUGujeH2X1J44j90R6tOBib+alhClVtTvG3kBV75mZSDyJ4O+jpEfHaXVhkLuRqNulj1 8/JwOQOreMdyIICK6Vll6GIio/5R8G2VyqapeU+4254fQKIyVwkahLaPr6SAUhJq77M8 lrT8RCJfFVoF2IrUonJIlDgROvIaqpiGAllrH+lsIzx3Z1uE2TStg1uhAXxBvY0lVJz9 mBzg== X-Received: by 10.50.78.231 with SMTP id e7mr911180igx.93.1450137269129; Mon, 14 Dec 2015 15:54:29 -0800 (PST) In-Reply-To: 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:100439 On Mon, Dec 14, 2015 at 4:48 PM, Vincent Davis wrote: > On Mon, Dec 14, 2015 at 4:14 PM, Cameron Simpson wrote: > >> First, notice that the code inside the try/except _only_ fetches the >> attribute. Your version calls the "write" attribute, and also accesses >> handle.name. Either of those might also emit AttributeError, and should >> probably not be silently caught. >> > > I think the intent of the original code was to check if handle had the > attribute "name", I don't think the attribute "write" was the issue. > > So then possibly this based on your suggestion: > try: > write = handel.write > except AttributeError: > raise Except that catching an exception just to immediately re-raise it is silly. This would be better: try: name = handle.name except AttributeError: pass else: handle.write("# Report_file: %s\n" % name)