<div dir="ltr"><div><br></div><div>As a user I always found a bit disurbing that dict pop method have a default while list and set doesn't. <br></div><div>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).</div><div>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.<br></div><div><br></div><div>We could for exemple ask ourselves wether or not <code class="gmail-descclassname">list.</code><code class="gmail-descname">index</code> should have a default, as it is a method that we explecitely excpect to return a value and might just raise instead.</div></div><div class="gmail_extra"><br><div class="gmail_quote">2018-10-31 2:08 GMT+01:00 Steven D'Aprano <span dir="ltr"><<a href="mailto:steve@pearwood.info" target="_blank">steve@pearwood.info</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">On Wed, Oct 31, 2018 at 02:25:25AM +0200, Serhiy Storchaka wrote:<br>
> 31.10.18 01:44, Giampaolo Rodola' пише:<br>
> >Sorry in advance if this has been proposed in the past but I couldn't <br>
> >find anything on python-ideas:<br>
> ><br>
> > >>> l = []<br>
> > >>> l.pop(default=1)<br>
> >1<br>
</span>[...]<br>
<span class=""><br>
> It is just<br>
> <br>
> l.pop() if l else default<br>
<br>
</span>It might *do* the same thing, but it doesn't communicate the <br>
programmer's intention as well.<br>
<br>
{}.pop('key', default) could be written using LBYL too, but the <br>
intention is much clearer given an explicit default argument.<br>
<br>
The only advantage of the "if l" version is that if the default is <br>
expensive to calculate, we can short-circuit it.<br>
<span class=""><br>
<br>
> or<br>
> <br>
> (l or [default]).pop()<br>
<br>
</span>That's clever, but it is also wasteful, building a single-item list only <br>
to immediately pop the item out of it and throw the list away.<br>
<br>
[steve@ando ~]$ python3.5 -m timeit -s "l = []" "l.pop() if l else None"<br>
10000000 loops, best of 3: 0.0739 usec per loop<br>
<br>
[steve@ando ~]$ python3.5 -m timeit -s "l = []" "(l or [None]).pop()"<br>
1000000 loops, best of 3: 0.421 usec per loop<br>
<span class="HOEnZb"><font color="#888888"><br>
<br>
<br>
-- <br>
Steve<br>
</font></span><div class="HOEnZb"><div class="h5">______________________________<wbr>_________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer" target="_blank">https://mail.python.org/<wbr>mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">http://python.org/psf/<wbr>codeofconduct/</a><br>
</div></div></blockquote></div><br><br clear="all"><br>-- <br><div class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><p>--<b><br>Nicolas Rolin</b> | Data Scientist<br>+ 33 631992617 - nicolas<a href="mailto:prenom.nom@tiime.fr" rel="nofollow" target="_blank">.rolin@tiime.fr</a></p><p><span></span><span><font color="#888888"><img src="https://docs.google.com/uc?export=download&id=0B5gEmxojZz7NUklic0RTMDVXd0E&revid=0B5gEmxojZz7NYytTZzQ3Q2t6d0xYZGZVSkljV3RCNGxZRENVPQ" width="96" height="28"> </font></span><br><i>15 rue Auber, </i><i>75009 Paris</i><br><i><a href="http://www.tiime.fr" rel="nofollow" target="_blank">www.tiime.fr</a></i></p></div></div></div></div></div></div>
</div>