logoalt Hacker News

anthonjtoday at 2:09 PM5 repliesview on HN

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.


Replies

courtcircuitstoday at 2:29 PM

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.

orthogonal_cubetoday at 3:09 PM

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.
bux93today at 2:32 PM

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.

show 2 replies
PaulDavisThe1sttoday at 2:25 PM

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) { ... }
rbanffytoday at 2:29 PM

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.