Path: csiph.com!news.mixmin.net!weretis.net!feeder1.news.weretis.net!news.roellig-ltd.de!open-news-network.org!feeder.erje.net!1.eu.feeder.erje.net!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 10:14:20 +1100 Lines: 58 Message-ID: References: Reply-To: python-list@python.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed X-Trace: news.uni-berlin.de OFayDMejMXdoeJwqsp9d8g/jNDdlcbQWfLJXqzuTQSHw== 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; 'anyway.': 0.04; 'preference': 0.05; 'used.': 0.05; 'cc:addr:python-list': 0.09; '%s\\n"': 0.09; 'attribute.': 0.09; 'spelling': 0.09; 'example:': 0.10; 'value.': 0.15; '"if"': 0.16; '>in': 0.16; 'accesses': 0.16; 'another?': 0.16; 'attribute,': 0.16; 'emit': 0.16; 'fetches': 0.16; 'from:addr:cs': 0.16; 'from:addr:zip.com.au': 0.16; 'from:name:cameron simpson': 0.16; 'iirc': 0.16; 'message-id:@cskk.homeip.net': 0.16; 'partly': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'simpson': 0.16; 'try/except': 0.16; 'weigh': 0.16; 'wrote:': 0.16; 'string': 0.17; 'attribute': 0.18; 'try:': 0.18; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'first,': 0.20; 'cheers,': 0.22; 'pass': 0.22; 'code,': 0.23; 'decide': 0.23; 'this:': 0.23; 'header:In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; 'error': 0.27; 'complain': 0.29; 'directly,': 0.29; 'second,': 0.29; 'code': 0.30; 'call.': 0.30; 'probably': 0.31; 'generally': 0.32; 'statement': 0.32; '(for': 0.34; 'except': 0.34; 'handle': 0.34; 'could': 0.35; 'but': 0.36; 'should': 0.36; 'there': 0.36; 'depends': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'charset:us- ascii': 0.37; 'version': 0.38; 'received:localdomain': 0.38; 'test': 0.39; 'whatever': 0.39; 'does': 0.39; 'your': 0.60; 'skip:u 10': 0.61; 'side': 0.62; 'more': 0.63; 'personal': 0.63; 'between': 0.65; 'cameron': 0.66; 'header:Reply-To:1': 0.67; 'reply-to:no real name:2**0': 0.71; 'as:': 0.79; 'internally.': 0.84; 'reply-to:addr:python.org': 0.84 Content-Disposition: inline In-Reply-To: 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:100435 On 14Dec2015 15:38, Vincent Davis wrote: >In the code below try is used to check if handle has the attribute name. It >seems an if statement could be used. Only by using hasattr(), which IIRC does a try/except internally. >Is there reason one way would be >better than another? try/except is more directly, but hasattr() is shorter. However, consider this: I would write your example: > try: > handle.write("# Report_file: %s\n" % handle.name) > except AttributeError: > pass > handle.write("########################################\n") as: try: write = handle.write except AttributeError: pass # or complain else: write("# Report_file: %s\n" % handle.name) write("########################################\n") Two things: 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. Second, notice that as a side effect of the: write = handle.write line we now have the attribute value. If it were an if: if hasattr(handle, 'write'): write = handle.write ... write messages now ... which is two steps to test and get the attribute. Feels... clunky. Perhaps partly because of the scope of a spelling error between the "'write'" string and the "handle.write" use below it, but clunky anyway. Which to use depends on how you weigh readability and directness. But try to get decide how mugh of your "if" preference is personal uncomfortableness with try/except (for whatever reasons), and then how much those reasons may be generally applicable or a personal foible. But it is your code, your call. What reads better to your eye? Cheers, Cameron Simpson