https://github.com/skifli/pyattr
Proper access modifiers for Python.
https://github.com/skifli/pyattr
access-control mangling python
Last synced: about 1 year ago
JSON representation
Proper access modifiers for Python.
- Host: GitHub
- URL: https://github.com/skifli/pyattr
- Owner: skifli
- License: mit
- Created: 2023-02-18T14:11:19.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-08-30T15:03:44.000Z (almost 3 years ago)
- Last Synced: 2025-04-29T18:53:22.486Z (about 1 year ago)
- Topics: access-control, mangling, python
- Language: Python
- Homepage: https://pypi.org/project/pyattr
- Size: 48.8 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# pyattr
[](https://pypi.org/project/pyattr)
[](https://pypi.org/project/pyattr/#files)

- [pyattr](#pyattr)
- [Installation](#installation)
- [Usage](#usage)
- [Example](#example)
- [How it works](#how-it-works)
- [Benchmarks](#benchmarks)
While Python does have name mangling, it is not nearly as powerful as access modifiers found in languages such as C++. **pyattr** provides an easy-to-use API for access modifiers in Python.
## Installation
Installation via **pip**:
```
pip install pyattr
```
## Usage
All you have to do is make your class inherit from the *`pyattr.Pyattr`* class, and add *`super().__init__()`* as the first line in the *`__init__`* function of your class. And that's it! **pyattr** will handle the magic to make sure variables cannot be accessed / set where the shouldn't be. It also provides useful error messages to users.
## Example
Here is a simple example involving a *private* variable.
```python
from pyattr import Pyattr
class Example(Pyattr):
def __init__(self) -> None:
super().__init__()
self.__name = "pyattr"
example = Example()
print(example.__name) # Error - '__name' is a private attribute of 'Example'.
```
As well as variables, **pyattr** also supports access control of *functions*!
```python
from pyattr import Pyattr
class Example(Pyattr):
def __init__(self) -> None:
super().__init__()
def __example(self) -> None:
pass
example = Example()
print(example.__example()) # Error - '__example' is a private attribute of 'Example'.
```
## How it works
> [!NOTE]
> For a more in-depth explanation on how **pyattr** works, see my [blog post](https://skifli.github.io/blog/2023/pyattr_in_depth_explanation.html#how-does-pyattr-work).
**pyattr** overrides the default *set* and *get* functions of your class. The overridden functions defined by **pyattr** are merged into your class when you inherit from the *`pyattr.Pyattr`* class. As well as this, the *`pyattr.Pyattr`* class inherits from the *`pyattr._PyattrDict`* class, which provides a custom dictionary implementation. This is because you can change the variables in a class using *`class.__dict__["var"] = "val"`*, meaning a custom dictionary would be the best way to prevent the access system being circumvented.
The overriden *set* and *get* functions of your class call the respective *set* and *get* functions of the custom dictionary. This dictionary, using *`sys._getframe()`*, works out the caller's function, and the caller's class (if any). It uses this data to work out if the caller should be allowed to access the specified variables. If it shouldn't, an *`AttributeError`* is raised, with an error message explaining the cause.
## Benchmarks
The code for the benchmarks can be found in the [benchmark](https://github.com/skifli/pyattr/blob/main/benchmark/) folder.
[](https://github.com/skifli/pyattr/blob/main/benchmark/bench_test.py)