It wasn't clear to me what you are proposing:

iter(an_int)

to return.

There have been multiple proposal in the past for it to return the same thing as:

iter(range(an_int)),

so that one could write:

for i in 23:
   ...

more compactly than:

for i in range(23):
   ...

but that proposal has been rejected many times.

On the other hand, maybe you are proposing that:

iter(5)

Should return the same thing as:

iter((5,))

which is really odd. It makes some small amount of sense if you assume that all sequences are "flat". But even then, the distinction between a single item and a sequence with one item in it a critical distinction that should not be masked.

Also - if you do this for integers, do you do it for all numbers? what about any other single object? (and THAT would get really strange with strings!)

BTW: I'm using `iter` here because that is the iteration protocol -- anything we do needs to conform to that.

- CHB




On Wed, Apr 14, 2021 at 1:35 AM Hans Ginzel <hans@matfyz.cz> wrote:
On Tue, Apr 13, 2021 at 05:39:42PM -0000, Dennis Sweeney wrote:
>Whenever you extend the definition of an operation (`__iter__` in this case) to more existing objects, you lose a little bit of the ability to catch errors early. Consider the function:
>
>    def traverse(something):
>        for x in something:
>            # do stuff
>            ...
>
>If you accidentally call `traverse(42)`, then right now, you catch it immediately with a TypeError on the `for x in something:` line. Under your proposal, you might just get a strange answer and not realize anything is wrong.

Error? Perhaps feature! You will get the desired behaviour and don't need to handle corner case like this.

from collections.abc import Iterable
def traverse(s):
   for x in s if isinstance(s, Iterable) else (s,):
     print(f"{x=}")

traverse(5)
traverse(range(3))

H.
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-leave@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/UIJRGCU4LUEZCNO4OUESKFVHPCSKHKKH/
Code of Conduct: http://python.org/psf/codeofconduct/


--
Christopher Barker, PhD (Chris)

Python Language Consulting
  - Teaching
  - Scientific Software Development
  - Desktop GUI and Web Development
  - wxPython, numpy, scipy, Cython