[Python-Dev] I'd like list.pop to accept an optional second
Tim Peters
tim_one@email.msn.com
Thu, 22 Jul 1999 23:08:02 -0400
[M.-A. Lemburg]
> Wouldn't a generic builtin for these kinds of things be
> better, e.g. a function returning a default value in case
> an exception occurs... something like:
>
> tryexcept(list.pop(), IndexError, default)
>
> which returns default in case an IndexError occurs. Don't
> think this would be much faster that the explicit try:...except:
> though...
As a function (builtin or not), tryexcept will never get called if
list.pop() raises an exception. tryexcept would need to be a new statement
type, and the compiler would have to generate code akin to
try:
whatever = list.pop()
except IndexError:
whatever = default
If you want to do it in a C function instead to avoid the Python-level
exception overhead, the compiler would have to wrap list.pop() in a lambda
in order to delay evaluation until the C code got control; and then you've
got worse overhead <wink>.
generalization-is-the-devil's-playground-ly y'rs - tim