Use Assigned Variable
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: use-assigned-variable
Section titled “Sourcery refactoring id: use-assigned-variable”Description:
Section titled “Description:”Uses a variable that was previously defined in the function instead of repeating what was defined in the variable
Before:
Section titled “Before:”wardrobe = {"blue_hat": 1, "red_hat": 3}for item in wardrobe: count = wardrobe[item] add_to_total(wardrobe[item])After:
Section titled “After:”wardrobe = {"blue_hat": 1, "red_hat": 3}for item in wardrobe: count = wardrobe[item] add_to_total(count)Explanation:
Section titled “Explanation:”Where possible, it is preferable to re-use local variables that have been assigned to. These variables will often have a more descriptive name that can aid in code comprehension, and additionally re-use can reduce duplication.