[Python-ideas] Keyword same in right hand side of assignments
R. David Murray
rdmurray at bitdance.com
Mon Mar 16 18:57:14 CET 2009
Heinrich W Puschmann <hwpuschm at yahoo.de> wrote:
>
> Python-Ideas: Keyword same in right hand side of assignments
> -------------------------------------------------------------------------------------------
>
> It is proposed to introduce a Keyword "same",
> to be used in the right hand side of assignments, as follows:
>
> "xx = same + 5" or "xx = 5 + same" synonymous with "xx += 5"
> "value = 2*same + 5" synonymous with "value =*2; value +=5"
> "switch = 1 - same" synonymous with "switch *-1; switch +=1"
> "lst = same + [5,6]" synonymous with "lst += [5,6]"
> "lst = [5,6] + same" synonymous with "lst = [5,6] + lst"
> "lst[2] = 1/same" synonymous with "lst[2] **=-1"
>
> and so on.
>
>
>
> IN GENERALITY, the effect of keyword same would be the following:
>
>
> The keyword same should only appear in
> the right hand side expression of an assignment,
> any number of times and wherever a name can appear.
> The left hand side of the assignment would have to be bound to some object.
>
> The keyword same is substituted by a name,
> which is bound to the object binding the left hand side of the assignment.
> The expression at the right hand side is evaluated.
> The left hand side identifyer is bound to the result of the expression.
Given your definition, this:
"lst = same + [5,6]" synonymous with "lst += [5,6]"
is not true. By your definition (and a programmer's naive expectation
based on other python semantics), lst = same + [5,6] translates to
lst = lst + [5,6]. But:
>>> old = lst = [1]
>>> lst = lst + [5,6]
>>> old, lst
([1], [1, 5, 6])
>>> old is lst
False
While:
>>> old = lst = [1]
>>> lst += [5,6]
>>> old, lst
([1, 5, 6], [1, 5, 6])
>>> old is lst
True
So the two are _not_ synonymous.
That point aside, I do not see the utility of this feature. To me,
it means that my eye would have to scan backward to the start of the
line to find out what 'same' was, whereas in the current formulation
the answer is right under my eyes. Code is more about reading that it
is about writing, since reading code is something done much more often
than writing it, so I'd rather keep it easier to read.
Personally I wouldn't find typing 'same' any easier than typing the
variable, anyway.
--
R. David Murray http://www.bitdance.com
More information about the Python-ideas
mailing list