New submission from Matthias welp: This is a suggestion, and not final.
The current ways to define the getter and setter methods for an attribute are these two: @property def name(): """ Docstring """ pass @name.setter def name(value): pass @name.deleter def name(value): pass and name = property(getter, setter, deleter, docstring) Of the two ways you can create a property, only the second one allows you to use your method definition multiple times, as seen here: value = property(getter, setter, deleter) value_2 = property(getter, setter, deleter) but this has the drawback that it can only use defined method. You can go around by defining a wrapper method, but even then you will have to put the value of your attribute inside the parenthesis of your behaviour definition, or put another assignation statement into your file. To prevent this, I propose the addition of decorators to attributes, which would behave like the decorators on functions: class Decorator(object): def __init__(self, ref): self.val = ref def __get__(self): return self.val def __set__(self, val): self.val = val def __del__(self): del self.val @Decorator val = value @Decorator val_2 = value_2 This should behave just like decorators on functions, but then with the functionality of attributes. In the proposed __init__ method ref would be the attribute after the evaluation of its declaration, similar to function decorators. The main benefit of this would be that the definition of attributes that behave in a similar way (get-, set- and delete-behaviour is the same) you only have to define one decorator for multiple attributes and you can change the behaviour of your attributes in a more intuitive way. The current alternatives are these: class Decorator(object): def __init__(self, ref): self.val = ref def __get__(self): return self.val def __set__(self, val): self.val = val def __del__(self): del self.val @Decorator def val(): pass @Decorator def val_2(): pass and def decorate(value): _value = value def get(): return _value def set(value): _value = value def del(): del _value return property(get, set, del) @decorate def val(): pass val_2 = decorate(1) val_3 = decorate() val_3 = 20 I think it is weird that to create an attribute that is decorated, I need to create a function, or need to explicitely call the decorator. This is why the decorators were added for functions, I think it might as well be added for attributes. ---------- components: Interpreter Core messages: 262710 nosy: Matthias welp priority: normal severity: normal status: open title: decorators for attributes type: enhancement _______________________________________ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue26681> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com