<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body><div><div style="font-family: Calibri,sans-serif; font-size: 11pt;">""getmany" doesn't tell you, many of what?"<br><br>Eh, neither does "get", but like I said, I like the idea regardless of colour.<br><br>Top-posted from my Windows Phone</div></div><div dir="ltr"><hr><span style="font-family: Calibri,sans-serif; font-size: 11pt; font-weight: bold;">From: </span><span style="font-family: Calibri,sans-serif; font-size: 11pt;"><a href="mailto:steve@pearwood.info">Steven D'Aprano</a></span><br><span style="font-family: Calibri,sans-serif; font-size: 11pt; font-weight: bold;">Sent: </span><span style="font-family: Calibri,sans-serif; font-size: 11pt;">5/27/2016 9:24</span><br><span style="font-family: Calibri,sans-serif; font-size: 11pt; font-weight: bold;">To: </span><span style="font-family: Calibri,sans-serif; font-size: 11pt;"><a href="mailto:python-ideas@python.org">python-ideas@python.org</a></span><br><span style="font-family: Calibri,sans-serif; font-size: 11pt; font-weight: bold;">Subject: </span><span style="font-family: Calibri,sans-serif; font-size: 11pt;">Re: [Python-ideas] Enhancing dict.values</span><br><br></div>On Fri, May 27, 2016 at 06:43:35AM -0700, Steve Dower wrote:<br>> Neat!<br>> <br>> My bikeshed is coloured `dict.getmany(*keys, default=None)`, <br><br>"getmany" doesn't tell you, many of what?<br><br><br>> and while <br>> returning a namedtuple might be cool, an iterator is probably the way <br>> to go.<br><br>The disadvantage of a namedtuple is that every invocation would create a <br>new class, which is then used once and once only for a singleton <br>instance. Could get very expensive.<br><br>I don't think an iterator would be needed. The motivating use-case is <br>for sequence unpacking:<br><br># apologies for breaking my own rule about realistic names<br>fee, fi, fo, fum = mydict.getmany('fee', 'fi', 'fo', 'fum')<br><br>so I don't think the lazy aspect of an iterator is useful. It's going to <br>be consumed eagerly, and immediately. I think a regular tuple is better. <br>That also matches the behaviour of itemgetter:<br><br>py> d = {'fe': 1, 'fi': 2, 'fo': 3, 'fum': 4}<br>py> from operator import itemgetter<br>py> f = itemgetter('fe', 'fi', 'fo', 'fum')<br>py> f(d)<br>(1, 2, 3, 4)<br><br>So the core functionality already exists, it's just hidden away in the <br>operator module. (Guido's time machine strikes again.)<br><br><br>Open questions:<br><br>- Is it worth making this a dict method? +1 from me.<br><br>- Name? "getvalues"?<br><br>- Any other functionality? <br><br>Possibly a keyword-only "default" argument:<br><br> mydict.getvalues(*keys, default=None)<br><br><br>On more shaky ground, how about a "pop" argument?<br><br> mydict.getvalues(*keys, pop=True)<br> <br>will delete the keys as well as return the values. Use-case: methods <br>(particularly __init__ or __new__) which take extra keyword args which <br>need to be popped before calling super.<br><br> def __init__(self, **kwargs):<br> colour, height = kwargs.getvalues('colour', 'height', pop=True)<br> super().__init__(**kwargs)<br> self.process(colour, height)<br><br><br><br><br>-- <br>Steve<br>_______________________________________________<br>Python-ideas mailing list<br>Python-ideas@python.org<br>https://mail.python.org/mailman/listinfo/python-ideas<br>Code of Conduct: http://python.org/psf/codeofconduct/<br></body></html>