https://github.com/avinashshenoy97/protecc
Access modifiers for python
https://github.com/avinashshenoy97/protecc
oop python
Last synced: 23 days ago
JSON representation
Access modifiers for python
- Host: GitHub
- URL: https://github.com/avinashshenoy97/protecc
- Owner: avinashshenoy97
- License: mit
- Created: 2019-05-30T05:21:57.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2019-06-19T17:49:19.000Z (about 7 years ago)
- Last Synced: 2025-12-16T16:39:39.869Z (6 months ago)
- Topics: oop, python
- Language: Python
- Size: 16.6 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# protecc
Access modifiers for python.
## Setup
#### PIP install
```bash
pip install protecc
```
#### From the source
```bash
git clone https://github.com/avinashshenoy97/protecc
cd protecc
python setup.py install
```
## Usage
### Old-Style Classes
Inherit from/extend the `protecc` class.
```python
from protecc import protecc
class regulatedClass(protecc):
def __init__(self):
self.publicVariable = 'public value'
self._privateVariable = 'private value'
def publicMethod(self):
return True
def __privateMethod(self):
return True
def getPrivateVariable(self):
return self._privateVariable
def privateMethodProxy(self):
return self.__privateMethod()
```
### New-Style Classes (type)
If you prefer metaclass (for better readability/what not), use `metaProtecc`.
```python
class metaRegulatedClass(metaclass=metaProtecc):
def __init__(self):
self.publicVariable = 'public value'
self._privateVariable = 'private value'
def publicMethod(self):
return True
def __privateMethod(self):
return True
def getPrivateVariable(self):
return self._privateVariable
def privateMethodProxy(self):
return self.__privateMethod()
```
When accessing "private" methods (i.e, methods that begin with either a single or two underscores), this exception is raised
```python
>>> r = regulatedClass()
>>> r.__privateMethod()
Traceback (most recent call last):
File "", line 1, in
File "/Users/avinashshenoy/STUFF/projects/protecc/protecc.py", line 23, in __protecced_getattribute__
raise AccessException('Cannot access ' + varType + ' member ' + name) from None
protecc.AccessException: Cannot access private member __privateMethod
```
Private methods can still be accessed from within other class methods, as is expected:
```python
>>> r = regulatedClass()
>>> r.privateMethodProxy()
True
```
### Note
- This is, by no means, fool-proof access protection.
- "Private members" are those members whose names are mangled by Python, as per PEP 8 conventions, i.e, starting with 2 underscores and having not more than one trailing underscore.
- Additionally, "protected members" are those members whose names are preceded by a single underscores. These members cannot be accessed from outside the class as well, as is expected.
## Primary Contributors
| | |
|:-:|:-:|
|
| [Avinash Shenoy](https://github.com/avinashshenoy97) |
#### License
This project is released under the [MIT License](https://github.com/avinashshenoy97/protecc/blob/master/LICENSE).