Home > Software design >  Is there any magic method for reflected augmented assignment?
Is there any magic method for reflected augmented assignment?

Time:11-17

I have two user-defined classes, a and b. For such an augmented addition assignment between them:

a  = b

Where a returns NotImplemented when it encounters b, is it expected that Python will look for a reflected augmented assignment in the other operand, i.e. b?

(Edited in response to comments)

I have noticed that already a slightly similar concept exists in some other magic methods, like __format__(self, other):

"A string {var:fmt_str}".format(var=b) === b.__format__("fmt_str")

CodePudding user response:

Nope. No reflected augmented assignment hooks. If __iadd__ fails, this operation falls back to __add__ and __radd__.

You can check the big list of numeric operator hooks in the data model docs to verify that there aren't any reflected augmented assignment hooks there.

  • Related