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

Ron Reiter ron.reiter at gmail.com
Wed Oct 31 05:36:04 EDT 2018


I think l.pop() if l else None is good enough. I think it's pretty obvious
that a developer means "Pop the list if the list is not empty, and return
None if the list is empty ".

- Ron


[image: Facebook] <http://www.facebook.com/ron.reiter> [image: Twitter]
<http://twitter.com/#!/ronreiter> [image: LinkedIn]
<https://il.linkedin.com/in/ronreiter>


On Wed, Oct 31, 2018 at 11:24 AM Nicolas Rolin <nicolas.rolin at tiime.fr>
wrote:

>
> As a user I always found a bit disurbing that dict pop method have a
> default while list and set doesn't.
> While it is way more computationally easy to check wether a list or a set
> is empty that to check if a key is in a dict, it still create a signature
> difference for no real reason (having a default to a built-in in python is
> pretty standard).
> It would be nice if every built-in/method of built-in type that returns a
> value and raise in some case have access to a default instead of raise, and
> not having to check the doc to see if it supports a default.
>
> We could for exemple ask ourselves wether or not list.index should have a
> default, as it is a method that we explecitely excpect to return a value
> and might just raise instead.
>
> 2018-10-31 2:08 GMT+01:00 Steven D'Aprano <steve at pearwood.info>:
>
>> On Wed, Oct 31, 2018 at 02:25:25AM +0200, Serhiy Storchaka wrote:
>> > 31.10.18 01:44, Giampaolo Rodola' пише:
>> > >Sorry in advance if this has been proposed in the past but I couldn't
>> > >find anything on python-ideas:
>> > >
>> > > >>> l = []
>> > > >>> l.pop(default=1)
>> > >1
>> [...]
>>
>> > It is just
>> >
>> >     l.pop() if l else default
>>
>> It might *do* the same thing, but it doesn't communicate the
>> programmer's intention as well.
>>
>> {}.pop('key', default) could be written using LBYL too, but the
>> intention is much clearer given an explicit default argument.
>>
>> The only advantage of the "if l" version is that if the default is
>> expensive to calculate, we can short-circuit it.
>>
>>
>> > or
>> >
>> >     (l or [default]).pop()
>>
>> That's clever, but it is also wasteful, building a single-item list only
>> to immediately pop the item out of it and throw the list away.
>>
>> [steve at ando ~]$ python3.5 -m timeit -s "l = []" "l.pop() if l else None"
>> 10000000 loops, best of 3: 0.0739 usec per loop
>>
>> [steve at ando ~]$ python3.5 -m timeit -s "l = []" "(l or [None]).pop()"
>> 1000000 loops, best of 3: 0.421 usec per loop
>>
>>
>>
>> --
>> Steve
>> _______________________________________________
>> Python-ideas mailing list
>> Python-ideas at python.org
>> https://mail.python.org/mailman/listinfo/python-ideas
>> Code of Conduct: http://python.org/psf/codeofconduct/
>>
>
>
>
> --
>
> --
> *Nicolas Rolin* | Data Scientist
> + 33 631992617 - nicolas.rolin at tiime.fr <prenom.nom at tiime.fr>
>
>
> *15 rue Auber, **75009 Paris*
> *www.tiime.fr <http://www.tiime.fr>*
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20181031/8a218b76/attachment-0001.html>


More information about the Python-ideas mailing list