Private Functions in Python

A robot chef holds a steaming plate of food for a customer while clutching a recipe card to its chest labelled “Recipe”.

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:

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.

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.

Main Topic

Python Modules and Packages

A sheep dressed as a chef happily pulls a recipe card labelled Python Modules from a wooden box of recipes on a table.

This tutorial introduces Python modules and packages, explaining their purpose, benefits, and everyday uses in keeping code organised, reusable, and efficient.

Introducing Python Exceptions

Flat illustration of a train diverted from a broken bridge by someone pulling a lever, symbolising Python Exceptions safety handling.

This tutorial introduces Python exceptions, explaining what they are, why they matter, and how handling them prevents program crashes.

Introduction to Python Sets

Cartoon elephant holding a football sticker book with unique players, pale lemon background, text reads “Python Sets” beside it.

A guide to Python sets, explaining uniqueness, usefulness, and everyday examples like sticker albums, shopping baskets, and mailing lists.

Introducing Python Lists

A cartoon frog smiles while writing “Python Lists” with items flies, worms, insects on a chalkboard in a classroom setting.

This is a Python series introducing lists, their importance, and everyday uses. Light-hearted explanations make coding concepts simple, clear, and fun.

Other Tutorials in this Topic

A friendly goat mechanic in denim overalls takes a screwdriver from a red toolbox filled with tools, with text "Python packages and modules".

Python Modules and Packages

This tutorial explains Python modules and packages, showing how to use them, structure folders, and import code…

A blue front door with a sign reading Python Module, beside a window showing cards labelled Version, Author, and Default Encoding.

Understanding __init__.py

This tutorial explains the purpose of `__init__.py` in Python packages, showing how it centralises variables, controls imports,…