[Python-ideas] Assignment decorators (Re: The Descriptor Protocol...)

Raymond Hettinger raymond.hettinger at gmail.com
Fri Mar 4 06:24:51 CET 2011

On Mar 3, 2011, at 7:14 PM, Greg Ewing wrote:
>> I don't think that assignment decorators add a significant new capability.  AFAICT, it just saves a few characters but doesn't change the way we think or work.
> As I've tried to point out, it's about more than just saving
> characters. It's about allowing the programmer to follow DRY,
> which is an important software engineering issue. It's also
> about improving readability, because that counts, you know.\

We both agree about the virtues of readability and DRY. I just disagree that either applies in the case of named tuples.

The following seems weird to me and causes a mental hiccup when reading it:

  Point = 'x y'       

The existing, Point = namedtuple('Point', 'x y') says what it does and my eyes just breeze over it.  So, I don't see a readability win.

Also, I don't think the DRY principle applies.  That is all about factoring-out a piece of knowledge so that it is used only once.  However, the use for named tuples is to define the class and then use that class name many times.  So, you still end-up using the name over and over again.  It is deeply embedded in the program.  You can't factor-out all of them with the proposed syntax, so you don't get any of the purported benefits of DRY:
   '''When the DRY principle is applied successfully, a modification of any 
   single element of a system does not change other logically-unrelated elements'''
To change the name of the class, you still have to do a global substitution, hitting the definition, every invocation, and its appearance in docs.  IOW, the class name is not a piece of knowledge that can be abstracted away.

Another software engineering principle, the "Rule of Three" suggests that the proposed abstraction doesn't meet the minimum threshold of usefulness.

Your idea may be valid and useful in other contexts, but named tuples aren't a good motivating use case, IMO.


P.S.  The current approach to assignments has the virtue that it is readable to people coming from other languages.  The decorator approach creates a piece of Python arcana that would only be decipherable to a smaller number of people.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20110303/b20ef772/attachment.html>

More information about the Python-ideas mailing list