2

Throwing exceptions is a relatively costly operation compared to the way other languages like C# and C++ handle iterators (and, it also makes it difficult for the programmer to use the iterators directly in python).

I rarely write any python code and when I do it's always verbose because I'm more used to the way languages closer to C work. I lack the ability to write those 1 line solutions python programmers are so proud of. I say this to make it clear that I do not know the design philosophy python follows. I can understand the rationale behind the design team's decisions for C#, and those of the C++ committee, even if I don't agree with all of those decisions, because I know the whole of the design, and I can see how everything fits together. Since that's not the case with Python, I'm left to wonder: why do iterators throw exceptions?

Davi
  • 398
  • 2
  • 12
  • 1
    How else do you suggest iterators tell the caller to stop iterating? – Devesh Kumar Singh Dec 29 '19 at 19:32
  • something like C#'s `HasNext` or C++'s `while(iterator != iterable.end())` – Davi Dec 29 '19 at 19:36
  • 3
    Perhaps the [PEP](https://www.python.org/dev/peps/pep-0234/) has those answers, also this is more of an opinion question, so it might be closed, Specifically the third point in `Resolved Issues` – Devesh Kumar Singh Dec 29 '19 at 19:37
  • 4
    Using exceptions for controlling the flow of a Python program is an accepted part of the language – Iain Shelvington Dec 29 '19 at 19:37
  • @DeveshKumarSingh I'm reading it. Since I'm not familiar with the language, it seems to me that backwards compatibility is the rationale behind that choice. – Davi Dec 29 '19 at 19:50
  • 1
    @DeveshKumarSingh Disagreed; [this question](https://stackoverflow.com/questions/21752259/python-why-pickle) is even more "opinion-based", yet well-received - and there are more examples. OP's question is defined enough. – OverLordGoldDragon Dec 29 '19 at 19:50
  • 1
    @Davi You've gotten unlucky with the question reception - don't mind it much, your question is fine. If it ends up being closed, you can tag me ('@' in comment) and I'll vote to reopen – OverLordGoldDragon Dec 29 '19 at 19:51
  • @OverLordGoldDragon sure, let's agree to disagree – Devesh Kumar Singh Dec 29 '19 at 19:53
  • Apparently, the issue of my question has been discussed before. I'm looking at the session "Resolved Issues" in the page linked to me by @DeveshKumarSingh. The explanations given make sense in a python dynamic typing context, but I don't know if maybe the C struct that represents a python object could be used to solve this problem (since there is no risk of the iterable containing the `end` value if that value is statically typed in C). But, like I said, I lack the knowledge to really know what's going on. – Davi Dec 29 '19 at 20:01
  • From what I gathered reading through a few PEPs, I think the philosophy of python design is to avoid automatically implementing too much stuff in C below the hood. The reason behind this could be keeping the OS dependant layer minimal, maybe because python has "being easy and fast to learn" as part of its mission, one could say that python aims to require minimal compliance to the C standard because, as an educational tool, it could be used as a learning language in very cheap and minimalistic devices. I may be **way** off, though... – Davi Dec 29 '19 at 20:33

0 Answers0