Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #100447

Re: Try: rather than if :

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Cameron Simpson <cs@zip.com.au>
Newsgroups comp.lang.python
Subject Re: Try: rather than if :
Date Tue, 15 Dec 2015 17:11:19 +1100
Lines 58
Message-ID <mailman.11.1450159883.22044.python-list@python.org> (permalink)
References <CALyJZZWn-UJPkMtNOBX9YcvAmWgA4qTo7_LwhHjciwevmrn1xw@mail.gmail.com>
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 CmI5iHiO46lpgorGTgYpHAIAuithmlzRHnhE5intltjQ==
Return-Path <cameron@cskk.homeip.net>
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; 'none:': 0.05; 'smallest': 0.07; 'cc:addr:python-list': 0.09; '%s\\n"': 0.09; 'attribute.': 0.09; 'logic': 0.09; 'name)': 0.09; 'name):': 0.09; 'def': 0.13; '>on': 0.16; '>try:': 0.16; 'accesses': 0.16; 'attribute,': 0.16; 'dropping': 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; 'message-id:@cskk.homeip.net': 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; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'first,': 0.20; 'issue.': 0.20; 'suggested': 0.20; 'cheers,': 0.22; 'pass': 0.22; 'code,': 0.23; 'bit': 0.23; 'dec': 0.23; 'header:In-Reply-To:1': 0.24; 'mon,': 0.24; 'header:User- Agent:1': 0.26; '14,': 0.27; 'this.': 0.28; 'raise': 0.29; 'code': 0.30; 'putting': 0.30; 'probably': 0.31; 'skip:_ 10': 0.32; 'possibly': 0.32; 'class': 0.33; 'case,': 0.34; 'except': 0.34; 'handle': 0.34; 'but': 0.36; 'should': 0.36; 'possible': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'say': 0.37; 'things': 0.38; 'version': 0.38; 'received:localdomain': 0.38; 'names': 0.38; 'stuff': 0.38; 'someone': 0.38; 'your': 0.60; 'needing': 0.63; 'cameron': 0.66; 'intent': 0.66; 'header:Reply- To:1': 0.67; 'reply-to:no real name:2**0': 0.71; 'clearer': 0.84; 'reply-to:addr:python.org': 0.84
Content-Disposition inline
In-Reply-To <CALyJZZWn-UJPkMtNOBX9YcvAmWgA4qTo7_LwhHjciwevmrn1xw@mail.gmail.com>
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 <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:100447

Show key headers only | View raw


On 14Dec2015 16:48, Vincent Davis <vincent@vincentdavis.net> wrote:
>On Mon, Dec 14, 2015 at 4:14 PM, Cameron Simpson <cs@zip.com.au> 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.

I have to say that this was not at all evident to me. I think that also argues 
for putting the smallest possible bit of code inside the try/except.

>So then possibly this based on your suggestion:
>try:
>    write = handel.write
>except AttributeError:
>    raise

Someone has already suggested dropping the try/except altogether for this.

>try:
>    name = handel.name
>    write("# Report_file: %s\n" % name)
>except AttributeError:
>    pass

Again, I would minimise the stuff in the try/except, so:

  try:
    name = handle.name
  except AttributeError:
    pass
  else:
    write("# Report_file: %s\n" % name)

But in this case, in my code, I do two things:

Firstly, things needing names always get one:

  class Foo:
    def __init__(self, blah, name=None):
      if name is None:
        name = "Foo-%s" % (id(self),)
      self.name = name

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)

The logic feels far clearer to my eye.

Cheers,
Cameron Simpson <cs@zip.com.au>

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Try: rather than if : Cameron Simpson <cs@zip.com.au> - 2015-12-15 17:11 +1100

csiph-web