A commonly repeated best practice is to not reuse local variables. However, when doing multiple small operations on the same variable, I struggle both with coming up with good names for all the variables, and I find that the multiple similar names hurt the readability.
Alternative 1: always create new variables
def clean_text(self, text: str) -> str: text_without_double_quotes = self._replace_double_quotes_with_single_quotes(text) text_without_double_quotes_and_foo = self._replace_foo_with_bar(text) text_without_double_quotes_and_foo_with_formatted_html = self._format_html(text) return text_without_double_quotes_and_foo_with_formatted_html
Alternative 2: reuse the variable
def clean_text(self, text: str) -> str: text = self._replace_double_quotes_with_single_quotes(text) text = self._replace_foo_with_bar(text) text = self._format_html(text) return text
Alternative 1 does have the advantage that I can set a breakpoint at the end of the method and inspect how the text was transformed in each step, but I am not sure if the tradeoff in readability is worth it or not.