3 ms·
As a programming hobbyist, one syntax I really like in Python, compared to C# and JS I've used, is it's for-in loop. 90%+ loops I used in my coding is to itera
by thrdbndndn 4y ago
As a programming hobbyist, one syntax I really like in Python, compared to C# and JS I've used, is it's for-in loop.
90%+ loops I used in my coding is to iterate elements in a list-like object. It feels very natural to use "for x in arr: do_sth(x)". And the same pattern can be reused with your typical "step increase by 1" loop by using `for i in range(LEN)`, instead of having to memorize another different pattern of `for (i=0; i<LEN; i++)`.
I particularly hate JS's forEach structure. for one, it doesn't follow how I think, and it's different from the other form of loops. And for two, I hate to type these pesky symbols (`a.forEach(e=>{doSth(e);})`) than just word(s).
Furthermore, JS also introduced `for in` and `for of`, which are great, but they're somewhat counterintuitive to my taste. You use `for ele of array` and `for index in array`, sounds fair. To iterate keys of objects, you use `for ... in`, but there is no `for ... of`. To make things worse, not all Array-like objects even support it. I find myself constantly "re-discover" (by that I mean I've google'd it multiple times before) the trick of things like `[...arr]`, and I hate it.
Some disclaimer: I know above is purely my preference, and not trying in anyway to say one is superior than the other. And I imagine for experienced programmers, they probably don't even think of all of these when using, it's just muscle memory. But for me, someone who code in Python/JS about half and half, I still struggle with, and have to google, basic syntax and structure in JS frequently, while I can do similar things without thinking in Python since when I was very new to the language. (And I know Python didn't invent these syntaxes.)