Tim Peters wrote: > > [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. Dang. You're right... > 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>. Oh well, forget the whole idea then. list.pop() is really not needed that often anyways to warrant the default arg thing, IMHO. dict.get() and getattr() have the default arg as performance enhancement and I believe that you wouldn't get all that much better performance on average by adding a second optional argument to list.pop(). BTW, there is a generic get() function in mxTools (you know where...) in case someone should be looking for such a beast. It works with all sequences and mappings. Also, has anybody considered writing list.pop(..,default) this way: if list: obj = list.pop() else: obj = default No exceptions, no changes, fast as hell :-) -- Marc-Andre Lemburg ______________________________________________________________________ Y2000: 162 days left Business: http://www.lemburg.com/ Python Pages: http://www.lemburg.com/python/
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4