Last active
June 9, 2017 12:34
-
-
Save DerevenetsArtyom/d924c26ea1dc04766b30a2f7567aaa2d to your computer and use it in GitHub Desktop.
[Custom Property] #python #decorators
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class myProperty: | |
def __init__(self, get_func=None, set_func=None, del_func=None): | |
self.get_func = get_func | |
self.set_func = set_func | |
self.del_func = del_func | |
def __get__(self, instance, cls): | |
if instance is None: | |
return self | |
if self.get_func is None: | |
raise AttributeError('can\'t get the attribute') | |
return self.get_func(instance) | |
def __set__(self, instance, value): | |
if self.set_func is None: | |
raise AttributeError('can\'t set the attribute') | |
return self.set_func(instance, value) | |
def __del__(self, instance): | |
if self.del_func is None: | |
raise AttributeError('can\'t delete the attribute') | |
return self.del_func(instance) | |
def getter(self, get_func): | |
return self.__class__(get_func, self.set_func, self.del_func) | |
def setter(self, set_func): | |
return self.__class__(self.get_func, set_func, self.del_func) | |
def deletter(self, del_func): | |
return self.__class__(self.get_func, self.set_func, del_func) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment