Skip to content

Top-level extract function

Extracts selected module-level Python statements into a new function while keeping the code explicit about inputs and outputs.

Before

raw_total = subtotal + tax
discounted_total = raw_total - discount
print(discounted_total)

After

def calculate_discounted_total(subtotal, tax, discount):
    raw_total = subtotal + tax
    discounted_total = raw_total - discount
    return discounted_total


discounted_total = calculate_discounted_total(subtotal, tax, discount)
print(discounted_total)

The action is available through PyCharm's extract method refactoring when the current selection is at module level and can be represented as a standalone function.