Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Add two dicts

Reply
Thread Tools

Add two dicts

 
 
Greg Brunet
Guest
Posts: n/a
 
      08-30-2003
"Greg Brunet" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> But what about something like this:
>
> >>> class xdict(dict):

> ... def __add__(self,dict2):
> ... result = self.copy()
> ... result = result.update(dict2)
> ...
>
> I was hoping that would allow:
> >>> a=xdict({'y': 456, 'x': 111})
> >>> b=xdict({'y': 444, 'z': 789})
> >>> a+b

>
> but instead of the result which I hoped for, I get the following:
>
> Traceback (most recent call last):
> File "<interactive input>", line 1, in ?
> TypeError: unsupported operand type(s) for +: 'xdict' and 'xdict'
>
> So I can't implement '+' operator for dictionaries - why not?



Ooops - that should have been:
return result.update(dict2)

in the last line, but still, shouldn't that have mapped the "+" operator
for xdict objects?

--
Greg

 
Reply With Quote
 
 
 
 
Greg Brunet
Guest
Posts: n/a
 
      08-30-2003
"Greg Brunet" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "Greg Brunet" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > But what about something like this:
> >
> > >>> class xdict(dict):

> > ... def __add__(self,dict2):
> > ... result = self.copy()
> > ... result = result.update(dict2)


.... I was getting sloppy in the interactive mode. Instead I did this &
it seems to work properly:


class xdict(dict):
def add(self,dict2):
result = self.copy()
result.update(dict2)
return result

def __add__(self,dict2):
result = self.copy()
result.update(dict2)
return result

def __iadd__(self,dict2):
self.update(dict2)
return self

a=xdict({'x':1})
b=xdict({'y':2})
print
print "Add:", a.add(b)
print "+:", a+b
print "a:",a,"b:",b
a+=b
print "+= (a=):", a

Results:
Add: {'y': 2, 'x': 1}
+: {'y': 2, 'x': 1}
a: {'x': 1} b: {'y': 2}
+= (a=): {'y': 2, 'x': 1}


--
Greg

 
Reply With Quote
 
 
 
 
John Roth
Guest
Posts: n/a
 
      08-30-2003

"Greg Brunet" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "Alex Martelli" <(E-Mail Removed)> wrote in message
> news:BjE3b.7850$(E-Mail Removed)...
> > Afanasiy wrote:
> >
> > > On Thu, 28 Aug 2003 22:07:06 -0700, Erik Max Francis

> <(E-Mail Removed)>
> > > wrote:
> > >
> > >>Afanasiy wrote:
> > >>
> > >>> Can I add two dicts in a way which is not cumbersome to the above

> %
> > >>> string
> > >>> operation? Is this another case of writing my own function, or

> does a
> > >>> builtin (or similar) already exist for this?
> > >>
> > >>combinedDict = aDict.copy()
> > >>combinedDict.update(anotherDict)
> > >>
> > >>If that's cumbersome, don't really know what you'd consider
> > >>non-cumbersome.
> > >
> > > Don't really know if you're asking, but :
> > >
> > > vars(self)+{'x':'123','y':'345'}
> > >
> > > I would consider that non-cumbersome.

> >
> > So, what about:
> >
> > def dict_add(adict, another):
> > result = adict.copy()
> > result.update(another)
> > return result
> >
> > and then dict_add(vars(self), {'x':'123','y':'345'}) ? But in fact
> > you can do even better...:
> >

> ... lots of other good ideas...
>
> But what about something like this:
>
> >>> class xdict(dict):

> ... def __add__(self,dict2):
> ... result = self.copy()
> ... result = result.update(dict2)
> ...
>
> I was hoping that would allow:
> >>> a=xdict({'y': 456, 'x': 111})
> >>> b=xdict({'y': 444, 'z': 789})
> >>> a+b

>
> but instead of the result which I hoped for, I get the following:
>
> Traceback (most recent call last):
> File "<interactive input>", line 1, in ?
> TypeError: unsupported operand type(s) for +: 'xdict' and 'xdict'
>
> So I can't implement '+' operator for dictionaries - why not?


The special functions can only be defined in the class
definition. They cannot be added afterwards. This is a
compiler optimization to avoid having to do dictionary
lookups on every operator.

If you want to do this, subclass dict.

John Roth
>
> --
> Greg
>
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Deep merge two dicts? Roy Smith Python 2 04-13-2012 03:02 PM
generating unique set of dicts from a list of dicts bruce Python 0 01-10-2012 08:24 PM
How to iterate through two dicts efficiently J Python 4 07-19-2011 05:25 PM
classes vs dicts Charlie Python 24 05-18-2004 10:57 AM
unicode keys in dicts Jiba Python 2 01-08-2004 03:20 PM



Advertisments