[Python-ideas] why not try without except?

Bruce Leban bruce at leapyear.org
Sun Apr 26 07:30:47 CEST 2009


The idea of making it easy blindly ignore all exceptions is not very
interesting.

On Sat, Apr 25, 2009 at 7:25 PM, Steven D'Aprano <steve at pearwood.info>wrote:

> On Sat, 25 Apr 2009 06:32:07 pm spir wrote:
>
> >    if hasattr(doc, 'footer')
> >       text += doc.footer
>
> A third alternative is to make sure that doc.footer always exists, even
> if it is only the empty string, and then just write:
>
> text += doc.footer
>
> In many cases, I prefer that. I don't like attributes which sometimes
> exist and sometimes don't.


It is a common case though and convenient to have a simple way to resolve
it. Adding a statement level ? operator would be a mistake in my opinion
because it clobbers the entire statement rather than just the specific. If
only :-) there were a way to get an attribute while at the same time
specifying the default value if the attribute didn't exist, say something
like:

    getattr(var, attribute [, default] )

Of course, I'd also it would make sense to also have:

    getitem(var, index [, default] )

--- Bruce
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20090425/7ad4012d/attachment.html>


More information about the Python-ideas mailing list