On Tue, 2021-04-13 at 17:00 +0200, Hans Ginzel wrote:
> Are there any reasons not to make scalar types iterable returning the
> value ones?
> Should each function check if it has got one value or a list/tuple
> before iteration over the argument?
> What is wrong on scalars for iteration, please?
> There is even legal to iterate over an empty set – an empty cycle.
> 

Iterating an empty set is well defined.  I can ask you to "move all
cars from this parking lot and park them in a row so that I can clean
them one by one".  It works just as well if there is no car to move.


There will probably be enough reasons why just adding it can break code
or how it would be confusing to do it only for numbers but not for
other python objects (assuming you limit this to numbers, since I
assume making all objects iterable like this would come with its own
problems).


Conceptually, my main argument is that iteration in Python (the
builtins) is typically "1-D".  That is, instead of an N-dimensional
container, a Python user will often use a nested list-of-lists. And
then iteration goes only over the first list.
If you iterate 0-D as "all elements", you should do so also for N-D in
my opinion (i.e. iterate all elements in a 2-D matrix).

This choice is inherited (for better or worse) by NumPy.  In principle,
if you designed everything around N-D objects, iterating all elements
by default seems like a great choice (I would probably prefer that for
NumPy, if I had the choice to go back in time).
In that case the "scalar" can be seen as a 0-D object and allowing to
iterate it is reasonable.
(Even then there are questions about mutability/being a container, so
it is not obvious to me, but we would get very close!)

That could (and likely should) be how projects like NumPy do things. 
But, it doesn't strike me as a good fit for the rest of the Python
builtins which mainly provides 1-D `sequences` without much of a
concept of N-D containers.

Cheers,

Sebastian


> Python 3.8.5 (default, Jan 27 2021, 15:41:15)
> > > > def chain(*iterables):
> ...   for iterable in iterables:
> ...     yield from iterable
> ...
> > > > a = 5
> > > > b = range(3)
> > > > for i in chain(a, b):
> ...   print(i)
> ...
> Traceback (most recent call last):
>    File "<stdin>", line 1, in <module>
>    File "<stdin>", line 3, in chain
> TypeError: 'int' object is not iterable
> 
> HG
> _______________________________________________
> Python-ideas mailing list -- python-ideas@python.org
> To unsubscribe send an email to python-ideas-le...@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/UTVIGXBPAWOAPOFMN526KKSUELRXAKXC/
> Code of Conduct: http://python.org/psf/codeofconduct/

Attachment: signature.asc
Description: This is a digitally signed message part

_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@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/7TNHBQD3GGLRELHKYCCGGJZFE5WZHQBJ/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to