array.'foo = bar
Is clearer. Or, at least, would be, if one were accustomed to the (fictitious) adverb '.There's no need for all the ceremony around specifying "for x in y: <some function describing what to do with x>"
array.'foo = bar
Is clearer. Or, at least, would be, if one were accustomed to the (fictitious) adverb '.There's no need for all the ceremony around specifying "for x in y: <some function describing what to do with x>"
for item in array:
item.foo = bar
for item in array:
item.foo = item.foo * 2
for item in array:
my_bar_func(item.foo)
If you special case certain uses of a for loop and add language syntax around it, of course you can make code terser. But it's nice to rely on a small set of very generally useful syntax.That’s also the curse: a generic loop can be almost anything (looping over an iterable can't quite express anything, but it can do a lot), so seeing it tells you nothing.
C-style loops can do more (literally anything), and loop-over-iterable syntax has been generally preferred where it can be applied because it tells you more (even if only a little more) about what the block of code is doing up-front.
Constructs that are more specific than loop-over-iterable can be correspondingly more expressive.
There's maybe an argument to be made about there being _too much_ notation to learn, but notation makes sense for fundamental concepts (which loops clearly are).
Notation is important. It condenses thought and refines the concepts, making them more visible. In fact, some valid mathematical theory came about from figuring out proper notation and then playing around with it, sometimes in non-rigorous (albeit intuitive ways). e.g. consider how physicists wildly move dx and dy around in differential equations (much to mathematicians' dismay)
Ancient greek geometry textbooks were written in long form: "the square of the hypotenuse side is equal to the sum of squares of the other two sides". We can all agree that the "a^2+b^=c^2" notation is better.
"Take each of my gizmos, then put it in the gizmo box"
for gizmo in gizmos: gizmo.put_in_box()
vs"Put all my gizmos in the gizmo box"
gizmos.'put_in_box()Your second sentence is ambiguous. It could mean
1. Put all of my gizmos in a single gizmo box, together, OR
2. Put each of my gizmos in its own gizmo box
> A wife asks her husband, a software engineer, “Could you please go shopping for me and buy one carton of milk, and if they have eggs, get 6!” A short time later the husband comes back with 6 cartons of milk. The wife asks him, “Why the hell did you buy 6 cartons of milk?” He replied, “They had eggs.”
"For every item in this truck, remove that item and put in in the warehouse."
"While this truck is not empty, keep unloading items to the warehouse."
What about:
"Can you unload the truck?"
The current context gives us implied knowledge that we're talking about the items in the truck and where it's getting unloaded to, such as a warehouse if we're near a warehouse door. We only expand on that verbally when we need to such as "Can you throw out the empty boxes in the truck?".
I think this is why programming is nonintuitive, you have to include important steps with very precise words that often go unsaid. Even "throw out" wouldn't convert to the correct intent if a computer executed that statement as is. You would end up literally throwing the empty boxes out of the truck instead of putting them into a dumpster or garbage can (the likely intent based on this example).
It's not _that_ verbose -- the trade off in clarity is pretty stark IMO.
In practice many people move between languages frequently daily. This syntax and semantics it provides are fairly different from that of popular languages.