pyattr
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.
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!
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.
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 folder.
Metadata
Release files for pyattr 1.4.6
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pyattr-1.4.6.tar.gz | 4.3 kB | Without provenance | Details for pyattr-1.4.6.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pyattr-1.4.6-py3-none-any.whl | Python 3 | none | any | Details for pyattr-1.4.6-py3-none-any.whl |
Total release size: 8.8 kB
Release files / pyattr-1.4.6.tar.gz
| Download URL | pyattr-1.4.6.tar.gz |
|---|---|
| Size | 4.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
cb2f4e850d9038b830b97d777e3130158c2b2cdb20f39b0298104d8ce9ae6635
|
|
BLAKE2b-256 checksum How to use checksums |
6a2675912a91568b7a42fc9ef432d88c0371b6f0676eb83f53fa9443dac8add3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 PyPy/7.3.11
|
Release files / pyattr-1.4.6-py3-none-any.whl
| Download URL | pyattr-1.4.6-py3-none-any.whl |
|---|---|
| Size | 4.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
60ae5e6df6e60f7f6dcd201e9b9af461f882a79a7327aeef90560772141a1c09
|
|
BLAKE2b-256 checksum How to use checksums |
8814f560dfe55a0e498cccfae267f767aa3850008d2ff5e5cbc49a36b49ae0d8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 PyPy/7.3.11
|
Security for pyattr 1.4.6
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.