dictionary - Overloading operators with dictionaries in Python -


i have class l takes multiple dictionaries this:

l((k:'apple', y:'3', j:'4'),(k:'2', f:'12', q:'cats')) 

i'm trying add 2 d objects , return new l object, such that

a = l((k:'apple', y:'3', j:'4'),(k:'2', f:'12', q:'cats')) b = l((n:'morapple', t:'23', f:'44'),(m:'14', n:'132', p:'morecats')) >> a+b l((k:'apple', y:'3', j:'4'),(k:'2', f:'12', q:'cats'),(n:'morapple', t:'23', f:'44'),(m:'14', n:'132', p:'morecats')) c = (c:'carrot',d:'2') >>a+c l((k:'apple', y:'3', j:'4'),(k:'2', f:'12', q:'cats'),(c:'carrot',d:'2')) 

i'm lost, far i've tried use eval() no success.. returns nothing , i'm not sure do?

first off, stay away eval() very, seldom idea use it.

secondly, why can't this:

def __add__(self, other):     if type(other) == dict:        return dl(*(self.dicts + [other]))     elif type(other) == dl:        return dl(*(self.dicts + other.dicts))     raise valueerror("only dl , dict supported")