Trying to understand += better

Roy Smith roy at panix.com
Sun Nov 22 21:31:07 EST 2009


If I've got an object foo, and I execute:

foo.bar += baz

exactly what happens if foo does not have a 'bar' attribute?  It's
pretty clear that foo.__getattr__('bar') gets called first, but it's a
little murky after that.  Assume for the moment that foo.__getattr__
('bar') returns an object x.  I think the complete sequence of calls
is:

foo.__getattr__('bar')  ==> x
x.__add__(baz)  ==> y
foo.__setattr__('bar', y)

but I'm not 100% sure.  It would be nice if it was, because that would
let me do some very neat magic in a system I'm working on :-)

How would things change if X defined __iadd__()?



More information about the Python-list mailing list