Skip to content

helper function

A helper function is a small function that handles one focused subtask on behalf of other code. Helpers keep the calling function short and readable by moving a chunk of logic behind a descriptive name.

Nothing in the language marks a function as a helper. A helper is an ordinary def that happens to serve another function, so the label describes intent. You’ll usually recognize one by its size and placement, since it does one narrow job and sits near the code that calls it.

When a helper is meant for use only inside its own module, the convention is to prefix its name with a single underscore, as in _clean_name(). PEP 8 calls this a weak “internal use” indicator, and it excludes the name from wildcard imports by default.

You can pull repeated or deeply nested logic into a helper, which is one of the most common refactorings. If only one function needs the helper, you can nest it inside that function.

Example

Say you’re normalizing usernames before storing them or looking them up. Both operations need the same cleanup, so that step goes into a helper:

Language: Python
>>> def _clean(name):
...     return name.strip().lower().replace(" ", "_")
...

>>> def register(name):
...     return f"created:{_clean(name)}"
...

>>> def lookup(name):
...     return f"found:{_clean(name)}"
...

>>> register("  Ada Lovelace ")
'created:ada_lovelace'
>>> lookup("ADA LOVELACE")
'found:ada_lovelace'

Here, _clean() does one small job, and both register() and lookup() delegate to it. The normalization rules live in a single place, and the underscore signals that _clean() isn’t part of what the module offers to the outside.

Defining Your Own Python Function

Tutorial

Defining Your Own Python Function

Learn how to define your own Python function, pass data into it, and return results to write clean, reusable code in your programs.

basics python

For additional information on related topics, take a look at the following resources:


By Martin Breuss • Updated Sept. 10, 2026