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


Groups > comp.lang.python > #100448

Re: Try: rather than if :

From Cameron Simpson <cs@zip.com.au>
Newsgroups comp.lang.python
Subject Re: Try: rather than if :
Date 2015-12-15 17:16 +1100
Message-ID <mailman.12.1450160195.22044.python-list@python.org> (permalink)
References <20151215061119.GA14163@cskk.homeip.net>

Show all headers | View raw


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>

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:16 +1100

csiph-web