Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Cameron Simpson Newsgroups: comp.lang.python Subject: Re: Try: rather than if : Date: Tue, 15 Dec 2015 17:16:31 +1100 Lines: 21 Message-ID: References: <20151215061119.GA14163@cskk.homeip.net> Reply-To: python-list@python.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit X-Trace: news.uni-berlin.de xkm8+xol+SL5NKT93f9yqAofrJfnDxOz6ZYGS8Oad/6g== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'cc:addr:python-list': 0.09; '%s\\n"': 0.09; 'name)': 0.09; 'name):': 0.09; '>on': 0.16; 'from:addr:cs': 0.16; 'from:addr:zip.com.au': 0.16; 'from:name:cameron simpson': 0.16; 'handle,': 0.16; 'message- id:@cskk.homeip.net': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'simpson': 0.16; 'wrote:': 0.16; 'attribute': 0.18; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'issue.': 0.20; 'cheers,': 0.22; 'this:': 0.23; 'header:In-Reply- To:1': 0.24; 'header:User-Agent:1': 0.26; 'code': 0.30; 'handle': 0.34; 'i.e.': 0.35; 'subject:: ': 0.37; 'received:localdomain': 0.38; 'your': 0.60; 'cameron': 0.66; 'intent': 0.66; 'header :Reply-To:1': 0.67; 'reply-to:no real name:2**0': 0.71; 'reply- to:addr:python.org': 0.84 Content-Disposition: inline In-Reply-To: <20151215061119.GA14163@cskk.homeip.net> User-Agent: Mutt/1.5.23 (2014-03-12) 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:100448 On 15Dec2015 17:11, Cameron Simpson wrote: >On 14Dec2015 16:48, Vincent Davis wrote: [...] >>​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. [...] >Secondly, for your use case "print the name if it has one" I _would_ use >hasattr: > > if hasattr(handle, name): > write("# Report_file: %s\n" % name) On reflection, there's also this: name = getattr(handle, 'name', repr(handle)) write("# Report_file: %s\n" % name) i.e. _always_ identfy the handle, even if it has no nice name. Cheers, Cameron Simpson