Skip to content

Merge Assignment and Augmented Assignment

Documentation Index

Fetch the complete documentation index at: https://docs.sourcery.ai/llms.txt

Use this file to discover all available pages before exploring further.

Sourcery refactoring id: merge-assign-and-aug-assign

Section titled “Sourcery refactoring id: merge-assign-and-aug-assign”

Replaces an assignment and an augmented assignment with a single assignment.

other_value = 33
number = 42
number += other_value
other_value = 33
number = 42 + other_value

When we mutate a variable multiple times without reading or writing its value in between, it’s more readable and more efficient to change its value only once. This way, it’s clearer which values this variable can have at various points.

This refactoring works with all 4 augmented assignment operators:

  • +=
  • -=
  • *=
  • /=