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


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

Re: Try: rather than if :

Started byCameron Simpson <cs@zip.com.au>
First post2015-12-15 17:16 +1100
Last post2015-12-15 17:16 +1100
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 : Cameron Simpson <cs@zip.com.au> - 2015-12-15 17:16 +1100

#100448 — Re: Try: rather than if :

FromCameron Simpson <cs@zip.com.au>
Date2015-12-15 17:16 +1100
SubjectRe: Try: rather than if :
Message-ID<mailman.12.1450160195.22044.python-list@python.org>
On 15Dec2015 17:11, Cameron Simpson <cs@zip.com.au> wrote:
>On 14Dec2015 16:48, Vincent Davis <vincent@vincentdavis.net> 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 <cs@zip.com.au>

[toc] | [standalone]


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


csiph-web