[Python-Dev] I'd like list.pop to accept an optional second

Tim Peters tim_one@email.msn.com
1999年7月26日 00:58:31 -0400


[M.-A. Lemburg]
> ...
> 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

I like their succinctness too; count = dict.get(key, 0) is helpfully
"slimmer" than either of
try:
 count = dict[key]
except KeyError:
 count = 0
or
count = 0
if dict.has_key(key):
 count = dict[key]
> and I believe that you wouldn't get all that much better performance
> on average by adding a second optional argument to list.pop().

I think you wouldn't at *all*, except in Jim's novel case. That is, when a
list is empty, it's usually the signal to get out of a loop, and you can
either test
 if list:
 item = list.pop()
 else:
 break
today or
 item = list.pop(-1, marker)
 if item is marker:
 break
tomorrow. The second way doesn't buy anything to my eye, and the first way
is very often the pretty
 while list:
 item = list.pop()
if-it-weren't-for-jim's-use-i'd-see-no-use-at-all-ly y'rs - tim

AltStyle によって変換されたページ (->オリジナル) /