Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #16816
| References | <2e457709-b40a-4763-8a3b-0e0560e98ee1@n10g2000vbg.googlegroups.com> |
|---|---|
| Date | 2011-12-08 02:24 -0800 |
| Subject | Re: how to test attribute existence of feedparser objects |
| From | Chris Rebert <clp2@rebertia.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.3401.1323339874.27778.python-list@python.org> (permalink) |
On Thu, Dec 8, 2011 at 1:34 AM, HansPeter <hanspeter.sloot@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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
how to test attribute existence of feedparser objects HansPeter <hanspeter.sloot@gmail.com> - 2011-12-08 01:34 -0800
Re: how to test attribute existence of feedparser objects Chris Rebert <clp2@rebertia.com> - 2011-12-08 02:24 -0800
Re: how to test attribute existence of feedparser objects xDog Walker <thudfoo@gmail.com> - 2011-12-10 09:19 -0800
Re: how to test attribute existence of feedparser objects Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-12-10 17:51 +0000
csiph-web