Try: rather than if :

Cameron Simpson cs at zip.com.au
Tue Dec 15 01:16:31 EST 2015


On 15Dec2015 17:11, Cameron Simpson <cs at zip.com.au> wrote:
>On 14Dec2015 16:48, Vincent Davis <vincent at 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 at zip.com.au>



More information about the Python-list mailing list