The dict keys method has other benefits beyond iteration. For example, it provides a set-like interface.<br><br><div class="gmail_quote"><div dir="ltr">On Wed, Sep 12, 2018, 10:50 PM Elias Tarhini <<a href="mailto:eltrhn@gmail.com">eltrhn@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">
<div class="gmail_quote"><div dir="ltr">On Wed, Sep 12, 2018 at 4:42 PM Michael Selik <<a href="mailto:mike@selik.org" target="_blank">mike@selik.org</a>> wrote:</div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div>You want to have a 
Mapping that does not supply a keys method? What use case motivated your
 proposal?</div></blockquote><div><br></div><div>Yes, my proposal was to consider 
allowing __iter__() to subsume keys() entirely, for the reasons outlined 
in my second email -- which I'm just realizing was an accidental "reply 
one" to Alex Walters rather than a "reply all", yikes! Here it is,
duplicated:</div><div><br></div><div>
<div dir="auto"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="font-family:sans-serif" dir="auto">Ahh, no, I phrased my question a bit badly -- I'm not proposing that the keys() method be <i>removed</i> from what it exists on currently, but rather that this:</div><span class="m_-2116329194242215538gmail-im"></span><br><span class="m_-2116329194242215538gmail-im"><div dir="auto" style="font-family:sans-serif">> To be treated like a mapping everywhere, python requires that you define* a keys() method, so why not use it?</div></span><br><span class="m_-2116329194242215538gmail-im"></span><div dir="auto" style="font-family:sans-serif"><font face="sans-serif">be changed, such that only __iter__() and __getitem__() indicate a mapping rather than keys() and the same.</font> The latter method would then be optional, but not necessarily removed from the front-facing API.</div><div dir="auto" style="font-family:sans-serif"><font face="sans-serif"><br></font></div><div dir="auto" style="font-family:sans-serif"><font face="sans-serif">Granted,
 my only strong argument is that the ** unpacking operator depends on 
this method to do its job, and it's currently alone amongst Python's 
operators in depending on a non-dunder to do so; </font>that, combined 
with the point heading the below paragraph, is also why I'm only going 
after keys() — not items() or sequences' index() or any of the other 
normally-named-but-important methods.</div><div dir="auto" style="font-family:sans-serif"><br></div><div dir="auto" style="font-family:sans-serif">And
 given that __iter__() is already recommended to iterate over a 
mapping's keys, it doesn't seem illogical to me to just let it take that
 job officially (and in turn, I guess, to make keys() on stdlib/built-in
 types just return __iter__()). I don't believe there would be another 
expected use for it in a mapping, anyway, correct? (I haven't done the 
research here, but there could be third-party authors who use it to 
iterate over items(), given that “why does it iterate over just keys” a 
somewhat-recognizable discussion topic on such beginner forums as 
/r/learnpython... if so, this could be a good opportunity to 
force-standardize that behavior as well.)</div><div dir="auto" style="font-family:sans-serif"><br></div><div dir="auto" style="font-family:sans-serif">Does this clarify my intent? ...I don't doubt that there are roadblocks I'm still ignoring, though.</div><br><div>Eli</div></blockquote><div><br></div><div>However, Serihy's example of <font face="monospace,monospace">{**[0, 2, 1]}<font face="arial,helvetica,sans-serif">
 is so-plainly irreconcilable -- somewhat embarrassing for me to have
 missed it -- that I'm now reluctantly no longer in favor. (Well, really, 
I'm tempted to say <i>why not?</i>, but I do see that it wouldn't be a good thing overall.)</font></font></div><div><font face="monospace,monospace"><font face="arial,helvetica,sans-serif">And I still kind of feel that there should be a dunder involved somewhere in this, but nowhere near strongly enough to dispute that <i>"[t]he dunder methods are dunder methods because they are not generally 
directly useful. [There doesn't seem to be] a major problem with having the mapping 
api call keys()
<font face="monospace,monospace"><font face="arial,helvetica,sans-serif"><i> [...]</i></font></font>"</i>, as it's reasonable and rationalizes the current system well enough. Thank you for bearing with ;)<br></font></font></div></div>

</div></div>

<br></div><div class="gmail_extra"><br clear="all"><div><div class="m_-2116329194242215538gmail_signature" data-smartmail="gmail_signature"><div dir="ltr">Eli<br></div></div></div></div><div class="gmail_extra">
<br><div class="gmail_quote">On Wed, Sep 12, 2018 at 4:42 PM, Michael Selik <span dir="ltr"><<a href="mailto:mike@selik.org" target="_blank">mike@selik.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Elias,<div>I'm a little confused about what you're suggesting. You want to have a Mapping that does not supply a keys method? What use case motivated your proposal?</div><div><br><br><div class="gmail_quote"><div><div class="m_-2116329194242215538h5"><div dir="ltr">On Mon, Sep 10, 2018, 7:04 PM Elias Tarhini <<a href="mailto:eltrhn@gmail.com" target="_blank">eltrhn@gmail.com</a>> wrote:<br></div></div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div class="m_-2116329194242215538h5"><div dir="auto"><div dir="ltr"><div>This has been bouncing around in my head for a while regarding the requisite 
<span style="font-family:monospace,monospace">keys()</span> method on mappings:</div><div><br></div><div>How come the ** unpacking operator, a built-in language feature, relies on a non-dunder to operate?</div><div><br></div><div>To me, I mean to say, requiring that classes implement <span style="font-family:monospace,monospace">keys()</span> – a method whose name is totally undistinguished – in order to conform to the mapping protocol feels like a design running counter to Python's norm of using dunders for everything "hidden". I am not sure if it feels dirty to anybody else, however. Interestingly, the docs <a href="https://docs.python.org/3/reference/datamodel.html#object.__iter__" rel="noreferrer" target="_blank">already say</a> that <i>[f]or mappings, [<span style="font-family:monospace,monospace">__iter__()</span>] should iterate over the keys of the container</i>, but it of course is not enforced in any way at present.<br></div><div dir="auto"><br></div><div dir="auto">So, then — how about enforcing it? Should <span style="font-family:monospace,monospace">__iter__()</span><span style="font-family:sans-serif">, for the reasons above, replace the current purpose of </span><span style="font-family:monospace,monospace">keys()</span><span style="font-family:sans-serif"> in mappings?</span></div><div dir="auto"><font face="sans-serif"><br></font></div><div dir="auto"><font face="sans-serif">I'm</font> not properly equipped at the moment to mess around with CPython (sorry), but 
I assume at a minimum this would entail either replacing all instances of <span style="font-family:monospace,monospace">PyMapping_Keys()</span> with <span style="font-family:monospace,monospace">PyObject_GetIter()<font face="arial,helvetica,sans-serif"> or alternatively changing  <span style="font-family:monospace,monospace">PyMapping_Keys()</span> to call the latter.</font></span></div><div><span style="font-family:monospace,monospace"><font face="arial,helvetica,sans-serif"><br></font></span></div><div><span style="font-family:monospace,monospace"><font face="arial,helvetica,sans-serif">Does it sound like a reasonable change overall?</font></span></div><div dir="auto"><span style="font-family:monospace,monospace"><font face="arial,helvetica,sans-serif"><br></font></span></div><div dir="auto"><span style="font-family:monospace,monospace"><font face="arial,helvetica,sans-serif">Eli</font></span></div></div></div></div></div><span>
_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org" target="_blank">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">http://python.org/psf/codeofconduct/</a><br>
</span></blockquote></div></div>
</blockquote></div><br></div>
</blockquote></div>