On Tue, Feb 28, 2017 at 5:26 PM, Michel Desmoulin <desmoulinmic...@gmail.com > wrote:
> Duck typing is precesily about incomplete but good enough similar API. > yes, though ideally one API is a subset of the other -- if they have the same method, it should mean the same thing: > For the dict and list: > > - you can iterate on both > 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! - you can index both > the indexing is only kinda the same, though, and you certainly can't slice dicts... > - you can size both > huh? what does sizing mean? you mean get the length? OK, that's similar. > Hence I can see very well functions working with both. E.G: helper to > extract x elements or a default value: > > def extract(data, *args, default="None"): > for x in args: > try: > yield data[x] > except (KeyError, ValueError): > yield default > > Usage: > > > a, b, c = extract(scores, "foo", "bar", "doh") > x, y, z = extract(items, 2, 5, 8, default=0) > really? when would you not know if your "keys" are indexes or arbitrary keys? or your data a sequence or mapping? I actually have this helper function. > > With list.get and tuple.get, this would become: > > def extract(data, *args, default="None"): > return (data.get(x, default) for x in args) > as a helper function, then it's OK if it's a bit more verbose. 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! -CHB -- Christopher Barker, Ph.D. Oceanographer Emergency Response Division NOAA/NOS/OR&R (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception chris.bar...@noaa.gov
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/