Core Concepts
Functions starting with an underscore in Python are not imported by default when using the wildcard import (from module import *) syntax.
Abstract
The author shares a lesson they learned about Python module imports. Typically, when using the wildcard import syntax (from module import *), all functions and variables defined in the module are imported into the current namespace. However, the author discovered that functions starting with an underscore (_) are not imported by default.
This behavior is by design in Python. Functions and variables starting with a single leading underscore (_) are considered "internal" or "private" and are not meant to be accessed directly from outside the module. The double leading underscore (__) is used for "name mangling" to avoid naming conflicts with subclasses.
The author provides a simple example to demonstrate this concept. They have two Python scripts, a.py and b.py, where a.py defines a regular function public_func() and a "private" function _private_func(). When importing all functions from a.py using from a import *, only public_func() is available, while _private_func() is not imported.
This knowledge can be useful when working with third-party libraries or when designing your own modules, to ensure that only the intended public API is exposed to the users of your code.