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


Groups > comp.lang.python > #100439 > unrolled thread

Re: Try: rather than if :

Started byIan Kelly <ian.g.kelly@gmail.com>
First post2015-12-14 16:53 -0700
Last post2015-12-14 16:53 -0700
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Try: rather than if : Ian Kelly <ian.g.kelly@gmail.com> - 2015-12-14 16:53 -0700

#100439 — Re: Try: rather than if :

FromIan Kelly <ian.g.kelly@gmail.com>
Date2015-12-14 16:53 -0700
SubjectRe: Try: rather than if :
Message-ID<mailman.5.1450137271.22044.python-list@python.org>
On Mon, Dec 14, 2015 at 4:48 PM, 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.
>
> 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)

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web