[Python-ideas] why not try without except?

Steven D'Aprano steve at pearwood.info
Sun Apr 26 09:10:25 CEST 2009

On Sun, 26 Apr 2009 03:30:47 pm Bruce Leban wrote:

> 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] )

But the OP isn't suggesting default values. See, for example, his 
discussion of ignoring any errors when the user doesn't supply a 
parameter to a function. If all he wanted was a default value, we can 
do that already.

> Of course, I'd also it would make sense to also have:
>     getitem(var, index [, default] )

Hmmm... interesting... 

Of course you can already do this:

    var[index:index+1] or [default]

if var is a list, and 

    var.get(key, default)

if it is a dictionary.

Steven D'Aprano

More information about the Python-ideas mailing list