Keyword same in right hand side of assignments
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. Since I am not a developer, I have no idea on how to difficult or how easy it would be to implement such a feature. As a programmer, however, I believe that it improves readability and user friendliness, and that it fully adjusts to the Python philosophy. It is very hard for me to discern, whether a similar idea has already been proposed by somebody else. I did not yet have any useful application for statements like "xx = xx.do(*args)" or "xx = yy.do(xx,*args)" or "xx = xx.do(xx,*args)" but as a matter of generalization, they should also allow subtitution of the right hand side appearances of xx by the keyword same. Heinrich Puschmann, Ulm
Heinrich W Puschmann <hwpuschm@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
On Tue, 17 Mar 2009 03:37:59 am Heinrich W Puschmann 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.
What's the point? Why not just write the following? xx = xx + 5 value = 2*value + 5 switch = 1 - switch lst = lst + [5,6] lst = [5,6] + lst lst[2] = 1/lst[2] What value do we gain by creating a new keyword that obscures what the assignment does? -- Steven D'Aprano
Heinrich W Puschmann 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"
Only if + is commutative and xx in immutable, and even then, see below.
"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.
This is an intriguing idea, which seems to extend the idea of augmented assignment, and which would seem most useful for complicated target expressions or for multiple uses of the pre-existing object bound to the target. It is similar in that the target must have a pre-existing binding. While it might work best for one target, it might not have to be so restricted. But one problem is that it changes and complicates the meaning of assignment statements. Currently, target-expression(s) = source-expression means evaluate source-expression; then evaluate target expression (s, left to right) and bind it (them) to the source object (or objects produce by iterating). This proposal requires that target be evaluated first, resolved to a object, bound to 'same' (or the internal equivalent) and after the assignment, unbound from 'same'. Since targets are expressions and not objects, I believe the target expression would have to be re-evaluated (without major change to the virtual machine) to make the binding, so this constructions would not save a target evaluation and would not be synonymous with augmented assignment even if it otherwise would be. So lst[2] = 1/same would really be equivalent to ____ = lst[2]; lst[2] = 1/____; del ____ Another problem is that any new short keyword breaks code and therefore needs a strong justification that it will also improve a substantial amount of other code. Terry Jan Reedy
participants (4)
-
Heinrich W Puschmann
-
R. David Murray
-
Steven D'Aprano
-
Terry Reedy