Maybe dictionary unpacking would be a nice thing?
d = {'foo': 42, 'egg': 23} {'foo': bar, 'egg': spam} = d print bar, spam 42 23
What do you think? Bad idea? Good idea? -panzi
On Mon, Mar 31, 2008, Mathias Panzenb?ck wrote:
Maybe dictionary unpacking would be a nice thing?
d = {'foo': 42, 'egg': 23} {'foo': bar, 'egg': spam} = d print bar, spam 42 23
What do you think? Bad idea? Good idea?
Horrible idea. ;-) -- Aahz (aahz@pythoncraft.com) <*> http://www.pythoncraft.com/ "It is easier to optimize correct code than to correct optimized code." --Bill Harlan
Aahz schrieb:
On Mon, Mar 31, 2008, Mathias Panzenb?ck wrote:
Maybe dictionary unpacking would be a nice thing?
d = {'foo': 42, 'egg': 23} {'foo': bar, 'egg': spam} = d print bar, spam 42 23
What do you think? Bad idea? Good idea?
Horrible idea. ;-)
And your argumentation on this is?
2008/3/31, Mathias Panzenböck <grosser.meister.morti@gmx.net>:
Maybe dictionary unpacking would be a nice thing?
d = {'foo': 42, 'egg': 23} {'foo': bar, 'egg': spam} = d print bar, spam 42 23
Bad idea. It uncovers a lot of details for your brain to take care when doing that, for example:
d = {'foo': 42, 'egg': 23} {'not': bar, 'egg': spam} = d print bar ??? {'egg': bar, 'egg': spam} = d print bar ???
I would consider something like the following: bar, spam = d.multiple("foo", "egg") with the semantics of: bar, spam = [d[k] for k in ("foo", "egg")] Regards, -- . Facundo Blog: http://www.taniquetil.com.ar/plog/ PyAr: http://www.python.org/ar/
"Facundo Batista" <facundobatista@gmail.com> wrote in message news:e04bdf310804010613h56d11f19u5253b05602a14778@mail.gmail.com... -1 because the need is way to rare for new syntax support and because | bar, spam = [d[k] for k in ("foo", "egg")] already does what is wanted in easily understood code. tjtr
Facundo Batista schrieb:
2008/3/31, Mathias Panzenböck <grosser.meister.morti-hi6Y0CQ0nG0@public.gmane.org>:
Maybe dictionary unpacking would be a nice thing?
d = {'foo': 42, 'egg': 23} {'foo': bar, 'egg': spam} = d print bar, spam 42 23
Bad idea.
It uncovers a lot of details for your brain to take care when doing that, for example:
d = {'foo': 42, 'egg': 23} {'not': bar, 'egg': spam} = d print bar ??? {'egg': bar, 'egg': spam} = d print bar ???
I would consider something like the following:
bar, spam = d.multiple("foo", "egg")
with the semantics of:
bar, spam = [d[k] for k in ("foo", "egg")]
We also have bar, spam = itemgetter("foo", "egg")(d) if some functional form is preferred. Georg -- Thus spake the Lord: Thou shalt indent with four spaces. No more, no less. Four shall be the number of spaces thou shalt indent, and the number of thy indenting shall be four. Eight shalt thou not indent, nor either indent thou two, excepting that thou then proceed to four. Tabs are right out.
Maybe just extend the functionality of the __getitem__ and __setitem__ methods of dicts?
class xdict(dict): def __getitem__(self,key): if type(key) in (tuple, list): return (dict.__getitem__(self,k) for k in key) else: return dict.__getitem__(self,key)
def __setitem__(self,key,value): if type(key) in (tuple, list): for k, v in zip(key,value): dict.__setitem__(self,k,v) else: dict.__setitem__(self,key,value)
d=xdict({'foo':23,'bar':42,'egg':'spam'}) d {'bar': 42, 'foo': 23, 'egg': 'spam'} d["foo",] <generator object at 0x9dbc02c> list(d["foo",]) [23] list(d["foo","bar"]) [23, 42] a,b=d['foo','egg'] a,b (23, 'spam') d['baken','bar'] = 'tomato', 36 d {'bar': 36, 'foo': 23, 'egg': 'spam', 'baken': 'tomato'}
Well, this would break current behaviour, so actually no. Not good. But maybe that way (no conflict because lists are unhashable):
class xdict(dict): def __getitem__(self,key): if isinstance(key,list): return (dict.__getitem__(self,k) for k in key) else: return dict.__getitem__(self,key)
def __setitem__(self,key,value): if isinstance(key,list): for k, v in zip(key,value): dict.__setitem__(self,k,v) else: dict.__setitem__(self,key,value)
d=xdict({'foo':23,'bar':42,'egg':'spam'}) list(d[["foo"]]) [23] list(d[["foo","bar"]]) [23, 42] a,b=d[['foo','egg']] a,b (23, 'spam') d[['baken','bar']] = 'tomato', 36 d {'bar': 36, 'foo': 23, 'egg': 'spam', 'baken': 'tomato'}
The [[ ]] looks almost like a spacial syntax. Good or bad? -panzi
On 31 Mar, 16:17, Mathias Panzenböck <grosser.meister.mo...@gmx.net> wrote:
Maybe dictionary unpacking would be a nice thing?
>>> d = {'foo': 42, 'egg': 23} >>> {'foo': bar, 'egg': spam} = d >>> print bar, spam 42 23
What do you think? Bad idea? Good idea?
I think this is one of the oddest things I've ever seen proposed until now. :) Actually I realized what was that for only after having seen Facundo's list comprehension translation.
participants (7)
-
Aahz
-
Christian Heimes
-
Facundo Batista
-
Georg Brandl
-
Giampaolo Rodola'
-
Mathias Panzenböck
-
Terry Reedy