[Python-ideas] Decorators for variables

Michel Desmoulin desmoulinmichel at gmail.com
Fri Apr 1 13:44:21 EDT 2016



Le 01/04/2016 19:33, Ian Kelly a écrit :
> (Resending to correct list. Sorry about that.)
> 
> On Fri, Apr 1, 2016 at 11:25 AM, Ian Kelly <ian.g.kelly at gmail.com> wrote:
>> On Fri, Apr 1, 2016 at 11:14 AM, Matthias welp <boekewurm at gmail.com> wrote:
>>>> An example of the transformation would help here
>>>
>>> An example, that detects cycles in a graph, and doesn't do an update if
>>> the graph has cycles.
>>
>> Thanks.
>>
>>> class A(object):
>>>     def __init__(self, parent):
>>>         @prevent_cycles
>>>         self.parent = parent
>>
>> I think you'll find that this doesn't work. Properties are members of
>> the class, not of instances of the class.
>>
>>> This would prevent cycles from being created in this object A, and would
>>> make
>>> some highly reusable code. The same can be done for @not_none, etc, to
>>> prevent
>>> some states which may be unwanted.
>>
>> But you could accomplish the same thing with "self.parent =
>> prevent_cycles(parent)". So I'm still not seeing how the use of the
>> decorator syntax eliminates repetition.

Not saying I like the proposal, but you can argue against regular
decorators the same way:

@foo
def bar():
   pass

Is just:

bar = foo(bar)

But, I think the benefit for @decorator on functions is mainly because a
function body is big, and this way we can read the decorator next to the
function signature while on a variable, this just add another way to
call a function on a variable.

> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
> 


More information about the Python-ideas mailing list