[Python-ideas] Add "default" kwarg to list.pop()

INADA Naoki songofacandy at gmail.com
Thu Nov 1 23:58:37 EDT 2018


On Fri, Nov 2, 2018 at 4:45 AM Serhiy Storchaka <storchaka at gmail.com> wrote:
>
> 31.10.18 13:08, Antoine Pitrou пише:
> > +1 from me.  dict.pop() already has an optional default.  This is a
> > straight-forward improvement to the API and no Python programmer will
> > be surprised.
>
> list.pop() corresponds two dict methods. With argument it corresponds
> dict.pop(). But there are differences: dict.pop() called repeatedly with
> the same key will raise an error (or return the default), while
> list.pop() will likely return other item. Without argument it
> corresponds dict.popitem() which doesn't have an optional default.
>

I think there is one more important difference between dict and list.
dict has .get(key[, default]), but list doesn't have it.

If we add only `list.pop([default])`, it is tempting that using it even when
they don't have to remove the item.  Unnecessary destructive change is
bad.  It reduces code readability, and it may create hard bug.

If this proposal is adding `list.get([index[, default]])` too, I still -0.
I don't know how often it is useful.

Regards,

-- 
INADA Naoki  <songofacandy at gmail.com>


More information about the Python-ideas mailing list