Keyword same in right hand side of assignments (rev)

R. David Murray rdmurray at bitdance.com
Tue Mar 17 12:29:58 EDT 2009


Jacob Holm <jh at improva.dk> wrote:
> I believe that as soon as the left-hand side stops being a simple 
> variable and it is used in non-trivial expressions on the right-hand 
> side, using the keyword would help clarify the intent.  What I mean is 
> that the examples you should be looking at are more like:
> 
> A[n+1] = same*same + 1
> B[2*j].foo = frobnicate(same, same+1)
> ...
> 
> If you try expanding these into current python with minimal change in 
> semantics you will end up with something like
> 
> _1 = n+1
> _2 = A[_1]
> A[_1] = _2*_2 + 1
> del _1
> del _2

Um, I'm not following you.  Why would this not translate to:

    A[n+1] = A[n+1]*A[n+1] + 1

which I find more readable than the 'same' example. 

> _1 = B[2*j]
> _2 = _1.foo
> _1.foo = frobnicate(_2, _2+1)
> del _1
> del _2

I'd be looking for ways to refactor that code, no matter how you
write it.  But really,

    B[2*j].foo = frobnicate(B[2*j].foo, B[2*j].foo + 1)

is to my eyes much clearer than the 'same' version.  I had to scan
the 'same' version twice to figure out what it was doing, and then
a third time to make sure I had it right.  This version has more
characters and thus more visual clutter, but all of the information
needed to understand what it is doing is right under my eyes as
I scan it.  I don't have to look back to the beginning of the
statement to remember what the function arguments are.

> I still think that the cost of a new keyword is probably too high a 
> price to pay, but I like the idea.

Yeah, you have to make a really, _really_ compelling case to get
a new keyword added.  Something you can't do any other way, or at
least not without an awful lot of hassle.

--
R. David Murray           http://www.bitdance.com




More information about the Python-list mailing list