how to test attribute existence of feedparser objects

Chris Rebert clp2 at rebertia.com
Thu Dec 8 05:24:30 EST 2011


On Thu, Dec 8, 2011 at 1:34 AM, HansPeter <hanspeter.sloot at gmail.com> wrote:
> Hi,
>
> While using the feedparser library for downloading RSS feeds some of
> the blog entries seem to have no title.
>
>  File "build\bdist.win32\egg\feedparser.py", line 382, in __getattr__
> AttributeError: object has no attribute 'title'
>
> Is there a way to test the existence of an attribute?
>
> I can use an exception but like below to see whether it exists but
> this is a clumsy way since the function has to return the title.

hasattr(obj, attr_name)
See docs.python.org/dev/library/functions.html#hasattr

That said, sounds like it won't make much difference in the particular
case you mention.
Also, never use a bare "except:" clause, unless you know what you're
doing and have a really good reason. Do "except AttributeError" in
this case.

Cheers,
Chris
--
http://rebertia.com



More information about the Python-list mailing list