<div dir="ltr">On Tue, Feb 28, 2017 at 5:26 PM, Michel Desmoulin <span dir="ltr"><<a href="mailto:desmoulinmichel@gmail.com" target="_blank">desmoulinmichel@gmail.com</a>></span> wrote:<br><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Duck typing is precesily about incomplete but good enough similar API.<br></blockquote><div><br></div><div>yes, though ideally one API is a subset of the other -- if they have the same method, it should mean the same thing:<br></div><div><br> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
For the dict and list:<br>
<br>
- you can iterate on both<br>
</blockquote><div><br></div><div>But you get different things -- dicts iterate on the keys, which wold be the equivalent of lists iterating on the indexes -- no one wants that!<br><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">- you can index both<br>
</blockquote><div><br></div><div>the indexing is only kinda the same, though, and you certainly can't slice dicts...<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">- you can size both<br></blockquote><div><br></div><div>huh? what does sizing mean? you mean get the length? OK, that's similar.<br> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hence I can see very well functions working with both. E.G: helper to<br>
extract x elements or a default value:<br>
<br>
def extract(data, *args, default="None"):<br>
for x in args:<br>
try:<br>
yield data[x]<br>
except (KeyError, ValueError):<br>
yield default<br>
<br>
Usage:<br>
<br>
<br>
a, b, c = extract(scores, "foo", "bar", "doh")<br>
x, y, z = extract(items, 2, 5, 8, default=0)<br></blockquote><div><br></div><div>really? when would you not know if your "keys" are indexes or arbitrary keys? or your data a sequence or mapping? <br></div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
I actually have this helper function.<br>
<br>
With list.get and tuple.get, this would become:<br>
<br>
def extract(data, *args, default="None"):<br>
return (data.get(x, default) for x in args)<br>
<div class="HOEnZb"><div class="h5"></div></div></blockquote><div><br></div><div>as a helper function, then it's OK if it's a bit more verbose.<br><br></div><div>If you were to argue that you wouldn't need the helper function at all, then that might make sense, but this still seems a dangerous and hopefully rare thing to do!<br><br></div><div>-CHB<br><br></div><div><br></div></div>-- <br><div class="gmail_signature" data-smartmail="gmail_signature"><br>Christopher Barker, Ph.D.<br>Oceanographer<br><br>Emergency Response Division<br>NOAA/NOS/OR&R (206) 526-6959 voice<br>7600 Sand Point Way NE (206) 526-6329 fax<br>Seattle, WA 98115 (206) 526-6317 main reception<br><br><a href="mailto:Chris.Barker@noaa.gov" target="_blank">Chris.Barker@noaa.gov</a></div>
</div></div>