https://github.com/a2435191/python-attribute-access-modifiers
Add C#-like `{get; set;}` syntax to Python attributes.
https://github.com/a2435191/python-attribute-access-modifiers
access-modifiers oop-python python
Last synced: 20 days ago
JSON representation
Add C#-like `{get; set;}` syntax to Python attributes.
- Host: GitHub
- URL: https://github.com/a2435191/python-attribute-access-modifiers
- Owner: a2435191
- License: mit
- Created: 2021-08-15T03:08:37.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2021-08-18T03:33:25.000Z (over 4 years ago)
- Last Synced: 2025-10-26T13:06:55.648Z (5 months ago)
- Topics: access-modifiers, oop-python, python
- Language: Python
- Homepage: https://pypi.org/project/python-attribute-access-modifiers/
- Size: 8.79 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# python-attribute-access-modifiers
Tired of typing `@property` just to make a read-only instance variable? Find yourself wishing for C#'s `{get; set;}` syntax? Look no further than PAAM, which auto-generates property getters, setter, and deleters for you!
## Quickstart
Once installed, just use the overloaded `|` operator between an instance variable and one of the predefined constants `GET`, `SET`, or `DEL`. Make sure to subclass from `PaamBase`.
```python
from paam import SET, PaamBase
class Test(PaamBase):
def __init__(self, a: int):
self.a = a | GET # write to self.a (not self._a) because PAAM creates setters/getters/deleters *after* __init__ finishes
>>> obj = Test(15)
>>> obj.a
>>> 15
>>> obj.a = 5 # raises AttributeError: can't set attribute
```
Property access modifiers can also be chained (order doesn't matter):
```python
from paam import SET, GET, DEL, PaamBase
class Test(PaamBase):
def __init__(self, a: int):
self.a = a | GET | SET
>>> obj = Test(15)
>>> obj.a
>>> 15
>>> obj.a = 5
>>> obj.a
>>> 5
>>> del obj.a # raises AttributeError: can't delete attribute
```
## Caveats (all planned to be fixed in future release)
* The type of any variable "annotated" with `GET`, `SET`, or `DEL` is `_PropertyAccessModifierBase`. This means `mypy` support and IDE type hinting are most likely broken.
* No tests
* Error messages currently only show owner class