private-attribute-cpp

raw JSON →
1.4.11 verified Fri May 01 auth: no python

Provides a mechanism to define private attributes in C++ implementations exposed to Python via pybind11. Current version 1.4.11, released on PyPI. Development is active on GitHub.

pip install private-attribute-cpp
error ImportError: cannot import name 'PrivateAttribute' from 'private_attribute_cpp'
cause Missing or incorrect installation; the package may not have been built correctly.
fix
Reinstall the package: pip install --force-reinstall private-attribute-cpp
error ModuleNotFoundError: No module named 'pybind11'
cause Pybind11 is a required dependency not installed.
fix
pip install pybind11
gotcha The 'PrivateAttribute' object itself does not enforce privacy at the Python level; it's a marker for C++ extensions. The privacy is only enforced when accessed from C++ code.
fix Use Python name mangling (double underscore) for true Python-level privacy; rely on PrivateAttribute only for C++ integration.
gotcha If you import directly from 'private_attribute_cpp' without having pybind11 installed, you may get a cryptic error about missing shared library.
fix Ensure pybind11 is installed: pip install pybind11

Shows how to use PrivateAttribute to define a private attribute in a Python class.

from private_attribute_cpp import PrivateAttribute

# Define a class with a private attribute
class MyClass:
    def __init__(self):
        self.__private_attr = PrivateAttribute('private_value')

    def get_private_attr(self):
        return self.__private_attr

obj = MyClass()
print(obj.get_private_attr())  # Access via method