Collections of non-arbitrary objects ?
Michele Simionato
michele.simionato at gmail.com
Mon Jun 25 23:42:26 EDT 2007
On Jun 25, 8:21 pm, Marius Gedminas <mged... at gmail.com> wrote:
> On Jun 24, 2:12 pm, Bjoern Schliessmann <usenet-
>
> mail-0306.20.chr0n... at spamgourmet.com> wrote:
> > 7stud wrote:
> > > if hasattr(elmt, some_func):
> > > elmt.some_func()
>
> > Personally, I prefer
>
> > try:
> > elmt.some_func()
> > except AttributeError:
> > # do stuff
>
> That also hides attribute errors that occur within some_func. I think
> I'd rather know when elmt has an implementation of some_func that is
> buggy. Thus I prefer the hasattr version.
Or possibly
try:
do_func = elmt.some_func
except AttributeError:
do_stuff()
else:
do_func()
(internally hasattr is doing that anyway).
Michele Simionato
More information about the Python-list
mailing list