Remix.run Logo
anthonj 4 hours ago

I don't really get the point of the article. Even if I knew little about python, would be it surpsing that a language with no real basic types is probably abstracting a lot?

Even a simple i=0, i=i+1 is "hiding" a lot in python then.

courtcircuits 4 hours ago | parent | next [-]

To be fair, when I started learning CS, the `for x in y` syntax was cryptic to me because I was unfamiliar with concepts such as iterators & generators. `for(int i=0; i<len(y); i++)` made way more sense since there is no hidden logic (besides additions as you highlighted in your comment, but which I think is easier to have a grasp of). So I really wish I had read this article when I started my CS journey a couple of years ago.

bux93 3 hours ago | parent | prev | next [-]

In the author's mind, it's unexpected/amazing that 'for' can iterate over many types. But it's NOT unexpected/amazing that 'iter' can iterate over many types. I have no idea why.

It's not like 'for' is limited to counting in other languages. The grand-daddy in c does something until some condition is false, and that thing can equally be incrementing/decrementing a number or invoking some function. That's what a loop does in any case, it compiles down to a conditional jump (JNE/JE..)

Maybe his reason for astonishment is obscured by over-use of an LLM to 'enhance' the text.

goodmythical 3 hours ago | parent | next [-]

Why would it ever be surprising that I can y= [x,x,y,y] for x in y; x=y; return y;

and get [y,y,y,y]?

thaumasiotes 3 hours ago | parent | prev [-]

> It's not like 'for' is limited to counting in other languages. The grand-daddy in c does something until some condition is false

C 'for' is a while loop. It's strictly syntactic sugar for an already existing feature. And it's really, really transparent. `for(A; B; C) { do_stuff(); }` isn't just a while loop, it's this while loop:

    A;
    while(B) {
      do_stuff();
      C;
    }
Other languages have treated for as a separate concept from while. C isn't really informative in that case.
orthogonal_cube 3 hours ago | parent | prev | next [-]

It would be very surprising for somebody without a formal software development background and years of experience.

Looking back at 2015 when Python 2 was still supported, there was a lot of confusion for why Python 2 would create a tuple while Python 3 created a generator for the following statement:

  foo = (x for x in [10, 20, 30])
The blog post is trying to help fill in a gap of knowledge for anyone trying to understand more of what goes on behind the curtains.
PaulDavisThe1st 4 hours ago | parent | prev | next [-]

But it's not a Python thing. Rust is noted below, and there's also C++.

  std::container<T> container;

  for (std::container<T>::iterator i = container.begin(); i != container.end(); ++i) { ...} 

  auto iter = container.begin(); while (iter != container.end()) { ...; ++iter; }

  for (auto const & t : container) { ... }
tialaramex 14 minutes ago | parent [-]

Barry Revzin has a talk explaining about different kinds of iterator. The C++ iterator is a maximalist, very powerful and flexible idea which - as a result - also has lots of opportunities to set everything on fire. Their model was the C pointer, we can iterate a pointer, compare it against some sentinel "end" value, dereference it, but also we could index into it, decrement the pointer and go backwards, comparing against a "start" value, and we can write to a reference rather than just read from it.

This is far more than most iterator designs allow, and as I said if you needed more this is very welcome, but if you didn't it's probably bad news.

C++ can sort iterators for example, so long as they're suitable for indexing back and forth as the sort progresses. Rust's sort functions are defined on its slice types, which makes sense in practical terms but if what you've got isn't quite a slice but could meet the requirements in C++ that's a problem for you.

rbanffy 4 hours ago | parent | prev [-]

Very few people who use Python realize the loop is not just looking into the values but asking the values to produce an iterator. It's only when they outgrow this early stage that they are ready to understand how to make a finite iterator themselves.