On Wed, Aug 05, 2020 at 10:40:02PM +1200, Greg Ewing wrote:

> A considerable number of moons ago, I suggested that
> 
>     @my_property
>     fred = 42
> 
> should expand to
> 
>     fred = my_property("fred", 42)


That require two different rules for decorators:

@decorator over a `class name` or `def name` statement:

- execute the statement
- bind `name = decorator(name)`

@decorator over a binding `target = expression`:

- bind `target = decorator("target", expression)`

So we're adding significant complexity to the concept of "decorator".


> The point being to give the descriptor access to the name of
> the attribute, without having to repeat yourself.

There is a conflict here between DRY and Explicit Is Better Than 
Implicit. In the case of function and class decorators, the factor that 
tips it over the edge is not the repeating of the name, but that 
decorator syntax puts critical information about the object up front, 
near the signature, instead of hidden way down the bottom where is can 
be missed:

    @property
    def thing(self):
        # fifty lines of code


makes it obvious that `thing` is a property to even the most lazy and 
careless reader. If the call to property followed the method declaration 
and it's implementation, it would be far away from the signature, 
obscuring the fact that `thing` is not just a method but a property.

There is no such advantage for this suggested decorator syntax:

    name = my_property('name', *args, **kw)


is at worst a trivial violation of DRY, and it may not even be that[1], 
but is completely flexible in how many positional and keyword arguments 
it receives. Whereas:

    @my_property
    name = arg

takes twice as many lines, can only accept a single positional argument, 
loses on the "Explicit versus Implicit" question, and saves at best only 
a trivial and inconsequential DRY violation.

So unlike the function/class case, where the benefit is both two-fold 
and large, the benefit here is only single and tiny, and in my opinion, 
outweighed by the disadvantages.




[1] See discussion here:

http://wiki.c2.com/?DontRepeatYourself

in particular "It's okay to have mechanical, textual duplication". In 
this case, the single source of truth is the target name:

    name = my_property("name", arg)

with the duplication being a single word, mechanically copied into the 
same line. A minor annoyance, but not a DRY violation in any meaningful 
sense.


-- 
Steven
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/NPTHWHSHSFLHY47X3FUILHEZIRR2HOJH/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to