public: Python Module Visibility Decorator

2020.12.3 · maintenance · verified Fri Apr 17

The `public` library for Python, version 2020.12.3, provides a `@public` decorator to explicitly mark functions, classes, and variables as part of a module's public API. It automatically manages and replaces the module's `__all__` list, aiming to simplify explicit module visibility control. The library appears to be in maintenance mode, with its last release in late 2020.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to use the `@public` decorator for functions and how to alias them. It also illustrates that non-decorated functions remain private and cannot be imported.

# my_module.py
from public import public

@public
def hello_world():
    """A publicly exposed function."""
    return "Hello from the public module!"

def _private_function():
    """A private function, not exposed."""
    return "You shouldn't see this!"

@public(alias='my_custom_name')
def original_function_name():
    """A public function exposed under an alias."""
    return "This function has a custom public name."

# To use this module:
# from my_module import hello_world, my_custom_name
# print(hello_world())
# print(my_custom_name())
# try:
#     from my_module import _private_function # This will raise an AttributeError
# except AttributeError as e:
#     print(f"Error trying to import private function: {e}")

view raw JSON →