When writing Python code, not every function is meant to be used everywhere. Some are helpers that tidy things up behind the scenes. These are called private functions. In Python, privacy is based on naming conventions, not strict rules — but the idea is the same: mark what’s meant for others and what’s not.
Why use private functions?
Imagine you write a module with ten functions, but only two are really important for other scripts to use. The rest are helpers. Marking the helpers as private keeps your code clear, stops people relying on unstable parts, and gives you freedom to change them later.
Think of it like a restaurant menu — you show diners the meals, but not every little recipe step in the kitchen.
Try this:
- Look at one of your scripts and ask: which functions would I want others to call, and which should stay hidden?
Private functions in modules
In Python, you mark a function as private by starting its name with an underscore:
# text_tools.py
def title_case(s):
return " ".join(_cap(word) for word in s.split())
def _cap(word): # private helper
return word[:1].upper() + word[1:].lower()
Here, title_case is public, while _cap is private. You can still import _cap if you try, but the underscore signals: “this is for internal use only”.
Try this:
- Create a module with one public function and one private function. Import the module in another script and see which feels right to use.
Private functions in packages
For larger projects with packages (folders of modules), you can keep internal helpers in their own files and signal privacy with an underscore in the filename.
mytools/
__init__.py
parser.py # public
_helpers.py # private
# parser.py
from ._helpers import _clean
def parse(text):
return _clean(text).split()
Users of your package will use parse, not _clean. The underscore in _helpers.py tells them it’s not part of the public API.
Try this:
- Create a package with one module for public functions and another starting with
_for helpers. Import only the public one in your main script.
Recap
- Private functions keep your code tidy and your public API clear.
- Use a single underscore (
_) in names or filenames to mark things as private. - Public functions are the ones you want others to rely on; private functions are helpers that can change at any time.






