Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/salabim/easy_property
Intuitive way to define a Python property with getter, setter, deleter, gestter_setter and documenter decorators
https://github.com/salabim/easy_property
Last synced: 15 days ago
JSON representation
Intuitive way to define a Python property with getter, setter, deleter, gestter_setter and documenter decorators
- Host: GitHub
- URL: https://github.com/salabim/easy_property
- Owner: salabim
- License: mit
- Created: 2020-06-03T10:46:30.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2020-08-25T17:58:09.000Z (about 4 years ago)
- Last Synced: 2024-10-06T09:11:22.863Z (about 1 month ago)
- Language: Python
- Size: 32.2 KB
- Stars: 22
- Watchers: 5
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: license.txt
Awesome Lists containing this project
README
# easy_property
Intuitive way to define a Python property with getter, setter, deleter, getter_setter and documenter decorators.Normally when you want to define a property that has a getter and a setter, you have to do something like
```
class Demo:
def __init__(self, val):
self.a = val
@property
def a(self):
return self._a
@a.setter
def a(self, val):
self._a = val
```
IMHO, the `@a.setter` is an ugly decorator, and hard to remember.With the easy_property module, one can use the decorators
- getter
- setter
- deleted, as in:
```
class Demo:
def __init__(self, val):
self.a = val
@getter
def a(self):
return self._a
@setter
def a(self, val):
self._a = val
@deleter
def a(self):
print('delete')
del self._a
```
In contrast with an ordinary property, the order of getter, setter and deleter is not important.
And it is even possible to define a setter only (without a getter), just in case.With easy_property, you can even create a combined getter/setter decorator:
```
class Demo:
def __init__(self, val):
self.a = val
@getter_setter
def a(self, val=None):
if val is None:
return self._a
self._a = val
```
Finally, it is possible to add a docstring to the property, with the @documenter decorator:
```
class Demo:
def __init__(self, val):
self.a = val
@getter
def a(self):
return self._a
@documenter:
def a(self):
return "this is the docstring of Demo.a"
```Although this might not be always a good solution, I think in many cases this will make it easier and more intuitive to
define properties.