Try: rather than if :

Vincent Davis vincent at vincentdavis.net
Mon Dec 14 18:48:58 EST 2015


On Mon, Dec 14, 2015 at 4:14 PM, Cameron Simpson <cs at 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
try:
    name = handel.name
    write("# Report_file: %s\n" % name)
except AttributeError:
    pass
write("########################################\n")


Vincent Davis
720-301-3003



More information about the Python-list mailing list