Add two dicts
Greg Brunet
gregbrunet at NOSPAMsempersoft.com
Sat Aug 30 02:36:08 EDT 2003
"Greg Brunet" <gregbrunet at NOSPAMsempersoft.com> wrote in message
news:vl0gbotgl3sibf at corp.supernews.com...
> 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
More information about the Python-list
mailing list