Augmented Assignment

Semantics

  • Augmented assignment mapped to special set of methods
     __iadd__(self,other)       # self += other
     __isub__(self,other)       # self -= other
     __imul__(self,other)       # self *= other
  • Contrast to ordinary operators
     __add__(self,other)        # self + other
     __sub__(self,other)        # self - other
     __mul__(self,other)        # self * other
  • Behavior of x +=y may differ from x = x + y

Overloading

  • Can overload augmented assignment operators
  • Just provide implementation of __iadd__(), __isub__(), etc.
<<< O'Reilly OSCON 2001, New Features in Python 2, Slide 9
July 26, 2001, beazley@cs.uchicago.edu
>>>