checking for the existence of an attribute

Terry Reedy tjreedy at udel.edu
Sun Oct 12 17:02:00 EDT 2003


"Bob Roberts" <bobnotbob at byu.edu> wrote in message
news:c4e6b17d.0310120923.3618cb98 at posting.google.com...
> I'm sure there must be a better way to do this:
>
>         try:
>             if item.page:
>                 DoSomething()
>         except AttributError:
>             pass

This checks both the item.page exists and that it has a True value.
Is that exactly the check you want?  Catching attribute errors with
try..except a standard Python idiom.

> Is there a simple way to check if item has "page" as one of its
attributes?

enter 'help(hasattr)' at prompt or check lib ref 'builtin functions'.
If item is often missing a page, (>10% of tries), then explicit test
might be better.

Terry J. Reedy







More information about the Python-list mailing list