<div dir="ltr"><div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">As attrgetter/itemgetter might get heterogeneus data, I would expect a per-get default, not [just] a global default.<br></blockquote>+1<br><br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Perhaps something like:<br></blockquote><span style="font-family:monospace,monospace"></span><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><br></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><span style="font-family:monospace,monospace">>>> itemgetter(-1, 0, -2,</span><br><span style="font-family:monospace,monospace">...            default0="first argument default",</span><br><span style="font-family:monospace,monospace">...            default1=["second", "argument", "default"],</span><br><span style="font-family:monospace,monospace">...            default={"global": "default"},</span><br><span style="font-family:monospace,monospace">... )</span><span class="gmail-HOEnZb"></span><br></blockquote><div> </div>The keywords suffixed by indice are uncommon. Maybe we can use dedicated<span style="font-family:monospace,monospace"> object.</span><br><span style="font-family:monospace,monospace"><br>>>> itemgetter(<br>...     itemgetter.WithDefault(-1, </span><span style="font-family:monospace,monospace"><span style="font-family:monospace,monospace">"first argument default"</span>),<br>...     </span><span style="font-family:monospace,monospace"><span style="font-family:monospace,monospace">itemgetter.WithDefault(0, </span><span style="font-family:monospace,monospace"></span></span><span style="font-family:monospace,monospace"><span style="font-family:monospace,monospace"><span style="font-family:monospace,monospace">["second", "argument", "default"])</span></span><br></span></div><span style="font-family:monospace,monospace">...     -2  # has no default value<br></span><div><span style="font-family:monospace,monospace">... )<br><br></span></div><div><span style="font-family:arial,helvetica,sans-serif">Another possibility is that </span><span style="font-family:monospace,monospace">itemgetter becomes an object with `add_default` method.<br></span><br><span style="font-family:monospace,monospace"><span style="font-family:monospace,monospace">>>> getter = itemgetter(-1, 0, 2)<br>>>> </span></span><span style="font-family:monospace,monospace"><span style="font-family:monospace,monospace"><span style="font-family:monospace,monospace"><span style="font-family:monospace,monospace">getter.add_default(0, </span></span></span></span><span style="font-family:monospace,monospace"><span style="font-family:monospace,monospace"><span style="font-family:monospace,monospace"><span style="font-family:monospace,monospace"><span style="font-family:monospace,monospace"></span><span style="font-family:monospace,monospace"><span style="font-family:monospace,monospace">"first argument default"</span></span>)</span></span></span></span><br><span style="font-family:monospace,monospace"><span style="font-family:monospace,monospace">>>> </span></span><span style="font-family:monospace,monospace"><span style="font-family:monospace,monospace"><span style="font-family:monospace,monospace"><span style="font-family:monospace,monospace">getter.add_default(2, </span></span></span></span><span style="font-family:monospace,monospace"><span style="font-family:monospace,monospace"><span style="font-family:monospace,monospace"><span style="font-family:monospace,monospace"><span style="font-family:monospace,monospace"></span><span style="font-family:monospace,monospace"><span style="font-family:monospace,monospace">"third argument default"</span></span>)<span style="font-family:monospace,monospace"><span style="font-family:monospace,monospace"><br><br></span></span></span></span></span></span></div><div><span style="font-family:arial,helvetica,sans-serif">or simply default parameter should be a list</span><span style="font-family:monospace,monospace"><span style="font-family:monospace,monospace"><span style="font-family:monospace,monospace"><span style="font-family:monospace,monospace"><span style="font-family:monospace,monospace"><span style="font-family:monospace,monospace"><br><br>>>> </span></span></span></span></span></span><span style="font-family:arial,helvetica,sans-serif"><span style="font-family:monospace,monospace">itemgetter(-1, 0, 2,<br></span></span></div><div><span style="font-family:arial,helvetica,sans-serif"><span style="font-family:monospace,monospace">...            default=["first", </span></span><span style="font-family:arial,helvetica,sans-serif"><span style="font-family:monospace,monospace"><span style="font-family:monospace,monospace"><span style="font-family:monospace,monospace"><span style="font-family:monospace,monospace"><span style="font-family:monospace,monospace"><span style="font-family:monospace,monospace"><span style="font-family:monospace,monospace"></span></span></span></span></span></span><span style="font-family:arial,helvetica,sans-serif"><span style="font-family:monospace,monospace">itemgetter</span></span>.NoDefault, "third"])</span></span></div><div><span class="gmail-HOEnZb"><font color="#888888"></font></span></div><div><span class="gmail-HOEnZb"><font color="#888888"><br></font></span></div><div><span class="gmail-HOEnZb"><font color="#888888"></font></span></div><div><span class="gmail-HOEnZb"><font color="#888888"><br></font></span></div></div><div class="gmail_extra"><br><div class="gmail_quote">2018-05-08 1:37 GMT+02:00 Danilo J. S. Bellini <span dir="ltr"><<a href="mailto:danilo.bellini@gmail.com" target="_blank">danilo.bellini@gmail.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">(1)<span class=""><br><br>On 7 May 2018 at 01:07, Raymond Hettinger <span dir="ltr"><<a href="mailto:raymond.hettinger@gmail.com" target="_blank">raymond.hettinger@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">He could've written:<br>
<br>
    get = rpartial(getattr, 'foo', None)<br>
    return get(args) or get(config) or get(env)<br></blockquote></span></div><br></div><div class="gmail_extra"></div><div class="gmail_extra">That's somewhat hybrid, it would behave like:<br><span style="font-family:monospace,monospace"><br>lambda obj: getattr(obj, "foo", None)</span>,<br><br>but one might expect a "reversed partial" to have its arguments reversed as well, like:<br><span style="font-family:monospace,monospace"><span style="font-family:monospace,monospace"><br>lambda obj: </span>getattr(obj, None, <span style="font-family:monospace,monospace">"foo"</span>)</span>.<br></div><br>(2)<br><br><div class="gmail_extra">Why getattr can't accept keyword arguments?<br><br><span style="font-family:monospace,monospace">>>> getattr_zero = partial(getattr, default=0)<br>>>> getattr_zero({}, "blah")<br>Traceback (most recent call last):<br>  File "<stdin>", line 1, in <module><br>TypeError: getattr() takes no keyword arguments</span><br clear="all"></div><div class="gmail_extra"><br></div><div class="gmail_extra">Since partial/partialmethod can apply keyword arguments, allowing the "default=" keyword argument in getattr would be an alternative to the "rpartial".<br><br>(3)<br><br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">At one time, lambda was the one obvious way. [...]<br></blockquote><br></div><div class="gmail_extra">I really like lambdas, but there are some "closure gotchas" like:<br><br></div><span style="font-family:monospace,monospace">>>> for idx in indices:<br></span><div class="gmail_extra"><span style="font-family:monospace,monospace">...     do_something(lambda seq: <span style="font-family:monospace,monospace">seq</span>[idx])</span><br></div><div class="gmail_extra"><br></div><div class="gmail_extra">If the lambda is stored somewhere to be called after the loop ends (or after its iteration), the <span style="font-family:monospace,monospace">seq[idx]</span> would load the item with the last iterated index. This:<br><br><div class="gmail_extra"><span style="font-family:monospace,monospace">>>> for idx in indices:<br></span></div><div class="gmail_extra"><span style="font-family:monospace,monospace">...     do_something(itemgetter(idx))</span><br></div><div class="gmail_extra"><br></div><div class="gmail_extra">would behave more like this instead:<br></div><br><div class="gmail_extra"><span style="font-family:monospace,monospace">>>> for idx in indices:<br></span></div><div class="gmail_extra"><span style="font-family:monospace,monospace">...     do_something((lambda i: (lambda <span style="font-family:monospace,monospace">seq</span>: <span style="font-family:monospace,monospace">seq</span>[i]))(idx))</span><br></div><div class="gmail_extra"><br></div>Which I wouldn't call "the one obvious way".<br></div><div class="gmail_extra"><br>(4)<span class=""><br><br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
If itemgetter and attrgetter only did a single lookup, a default might 
make sense.  However, that doesn't fit well with multiple and/or chained
 lookups where are number of options are possible. (See <a href="https://bugs.python.org/issue14384#msg316222" rel="noreferrer" target="_blank">https://bugs.python.org/issue1<wbr>4384#msg316222</a> for examples and alternatives).</blockquote></span><div class="m_-231032742051817602gmail-yj6qo m_-231032742051817602gmail-ajU"><div id="m_-231032742051817602gmail-:1gn" class="m_-231032742051817602gmail-ajR"><img class="m_-231032742051817602gmail-ajT" src="https://ssl.gstatic.com/ui/v1/icons/mail/images/cleardot.gif"></div></div>As attrgetter/itemgetter might get heterogeneus data, I would expect a per-get default, not [just] a global default.<br>Perhaps something like:<br><span style="font-family:monospace,monospace"><br>>>> itemgetter(-1, 0, -2,<br>...            default0="first argument default",<br>...            default1=["second", "argument", "default"],<br>...            default={"global": "default"},<br>... )</span><span class="HOEnZb"><font color="#888888"><br><br></font></span></div><span class="HOEnZb"><font color="#888888"><div class="gmail_extra"></div>-- <br><div class="gmail_extra"><div class="m_-231032742051817602gmail_signature" data-smartmail="gmail_signature">Danilo J. S. Bellini<br>---------------<br>"<i>It is not our business to set up prohibitions, but to arrive at conventions.</i>" (R. Carnap)<br></div>
</div></font></span></div>
<br>______________________________<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>
<br></blockquote></div><br></div>