Operator overloading and __getattr__
Samuel Kleiner
sam at h41n2fls31o839.telia.com
Tue Dec 23 11:53:03 EST 2003
Bengt Richter wrote:
> On Mon, 22 Dec 2003 10:03:03 -0600, Samuel Kleiner <sam at h41n2fls31o839.telia.com> wrote:
>
>>I'm trying to create methods on method access- but __getattr__ fails
>>with operator overloading(below) Any suggestions? EG:
> First please explain why you are trying to do whatever it is you're trying to do ;-)
I was trying to automatically extend M to cover all binary operators not
covered by dict, preferably without assigning to them all.
class M(dict):
def __add__(self, add):
if type(add) in self:
self[type(add)]=self[type(add)]+add
else:
self[type(add)]=add
return self
def __str__(self):
return "+".join(map(str,self.values()))
print M()+"foo"+2+"bar"+5
Out:
foobar+7
>>Causes TypeError on the last line
> I doubt if that's the first problem ;-)
Yes, I was jetlagged. new.instancemethod seems the way to go, really.
--
Loren ipsum dolor sit amet, consectetuer adipiscing elit, sed diam
nonummy nibh eusmod tincidunt ut laoreet dolore magna aliquam erat
volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation
More information about the Python-list
mailing list