# Make int iterable returning itself?

**URL:** <https://discuss.python.org/t/make-int-iterable-returning-itself/101595>\
**Category:** Ideas\
**Created:** [August 7, 2025, 9:31pm UTC](https://discuss.python.org/t/make-int-iterable-returning-itself/101595 "2025-08-07T21:31:59Z")\
**Posts on this page:** 12\
**Page:** 2

<div class="post-metadata">

**Author:** ![timhoffm](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/timhoffm/32/19468_2.png) [@timhoffm](https://discuss.python.org/u/timhoffm)\
**Post date:** [August 10, 2025, 5:30am UTC](https://discuss.python.org/t/make-int-iterable-returning-itself/101595/21 "2025-08-10T05:30:52Z")

</div>

If I understand correctly, that‘s `SequenceNotStr[str]` from

> <https://github.com/hauntsaninja/useful_types/blob/f5e657236b561e4b87a63c9982e2bda0c791beca/useful_types/ __init__.py#L283-L285>

But we’re getting off topic with the string discussion. The question was whether integers should be iterable, and there I think the answer is clearly no. I claim it is rather unintuitive. Ask some people what this would do, and I think you won’t get clear answers

```python
for i in 5:
    print(i)

```

AFAICS the iterable idea originated from having mixed types

```python
month = 8 # or None or (7,8)

```

And the need to coerce int or sequence of int to a sequence before further processing. But I think it’s ok to do

```python
if not isinstance(month, tuple):
    month = (month,)

```

You will have to do this for any other type ( float, custom classes, …) anyway. There’s no benefit in introducing special logic to save this for ints.

---

<div class="post-metadata">

**Author:** ![hginzel](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/hginzel/32/25510_2.png) [@hginzel](https://discuss.python.org/u/hginzel)\
**Post date:** [August 10, 2025, 6:47am UTC](https://discuss.python.org/t/make-int-iterable-returning-itself/101595/22 "2025-08-10T06:47:07Z")

</div>

Thank you for the nesting lookup example.

> [@sirosen](#):
>
> Why that definition rather than the countless other viable definitions?

What other definitions do you suggest? `range(n)` has already been discussed and (correctly) rejected.

---

<div class="post-metadata">

**Author:** ![hginzel](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/hginzel/32/25510_2.png) [@hginzel](https://discuss.python.org/u/hginzel)\
**Post date:** [August 10, 2025, 6:58am UTC](https://discuss.python.org/t/make-int-iterable-returning-itself/101595/23 "2025-08-10T06:58:12Z")

</div>

I’ve already generally tested for `isinstance(month, Iterable)`in my code example.

> [@timhoffm](#):
>
> You will have to do this for any other type (float, custom classes, …) anyway. There’s no benefit in introducing special logic to save this for ints.

It is not _special_ logic — it’s just filling in the missing iterating over _scalar_ in a consistent way with single character / string.

---

<div class="post-metadata">

**Author:** ![timhoffm](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/timhoffm/32/19468_2.png) [@timhoffm](https://discuss.python.org/u/timhoffm)\
**Post date:** [August 10, 2025, 7:06am UTC](https://discuss.python.org/t/make-int-iterable-returning-itself/101595/24 "2025-08-10T07:06:22Z")

</div>

> [@hginzel](#):
>
> It is not _special_ logic — it’s just filling in the missing iterating over _scalar_ in a consistent way with single character / string.

That‘s a point of view. I claim that iterable single character / string is the odd ball, but that‘s rooted in that we do not have a concept of character. Characters are just strings of length 1. That has the side effect, that you can iterate over it. It’s not a first class feature intentionally designed. This is not comparable to integers. They are not first and foremost a sequence, which has elements that itself are of the same type.

More generally, what is _scalar_ in your context? Is it only int or also float, a custom class, …?

---

<div class="post-metadata">

**Author:** ![Stefan2](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/stefan2/32/18492_2.png) [@Stefan2](https://discuss.python.org/u/Stefan2)\
**Post date:** [August 10, 2025, 10:28am UTC](https://discuss.python.org/t/make-int-iterable-returning-itself/101595/25 "2025-08-10T10:28:49Z")

</div>

> [@hginzel](#):
>
> What other definitions

Another perhaps reasonable definition would provide the digits. Then for example for digit sum we could stop writing `sum(map(int, str(n)))` and just do `sum(n)`.

---

<div class="post-metadata">

**Author:** ![xitop](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/xitop/32/13242_2.png) [@xitop](https://discuss.python.org/u/xitop)\
**Post date:** [August 10, 2025, 11:20am UTC](https://discuss.python.org/t/make-int-iterable-returning-itself/101595/26 "2025-08-10T11:20:59Z")

</div>

If we don’t want to break almost all existing “flatten nested structure” functions, we could only add something like `int.as_tuple()`, or in the case of the previous example `int.as_digits()`.

---

<div class="post-metadata">

**Author:** ![hginzel](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/hginzel/32/25510_2.png) [@hginzel](https://discuss.python.org/u/hginzel)\
**Post date:** [August 10, 2025, 11:35am UTC](https://discuss.python.org/t/make-int-iterable-returning-itself/101595/27 "2025-08-10T11:35:09Z")

</div>

> [@timhoffm](#):
>
> That‘s a point of view. I claim that iterable single character / string is the odd ball,
> 
> More generally, what is _scalar_ in your context? Is it only int or also float, a custom class, …?

I consider iterating over single character as a feature and missing iterating over scalar (int, float, complex, bool, NoneType – all built-in scalar types) could be added (returning scalar).

Still open question is: What can happen/break by adding this?  
Besides potential existing “flatten nested structure” functions.

---

<div class="post-metadata">

**Author:** ![TomFryers](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/tomfryers/32/11086_2.png) [@TomFryers](https://discuss.python.org/u/TomFryers)\
**Post date:** [August 10, 2025, 5:00pm UTC](https://discuss.python.org/t/make-int-iterable-returning-itself/101595/28 "2025-08-10T17:00:04Z")

</div>

If you have a single integer, then you can use `int`. If you have some unknown number of integers, you can use (e.g.) `list[int]`. `[5]` exactly models the concept of ‘I might have more than one number, but as it happens, I only have the one: five’.

Letting single numbers behave like collections muddys the waters, and can easily mask bugs:

```python
from functools import reduce
import operator

def product(xs):
    return reduce(operator.mul, xs, 1)

def geometric_mean_calf_age(cow):
    n = len(cow.calves)
    return product(cow.age) ** (1 / n)
    # Oops, I meant
    # return product(calf.age for calf in cow.calves) ** (1 / n)

```

The `TypeError` becomes a silent wrong answer.

Mathematics habitually conflates scalars with single-element vectors and matrices. That’s not much of a problem, because, as a mathematician, you can always clarify when necessary. (Though few bother.) But to have a language undermine the distinction _for you_, everywhere, whether you like it or not, is much worse.

---

<div class="post-metadata">

**Author:** ![MRAB](https://avatars.discourse-cdn.com/v4/letter/m/e9c0ed/32.png) [@MRAB](https://discuss.python.org/u/MRAB)\
**Post date:** [August 10, 2025, 6:07pm UTC](https://discuss.python.org/t/make-int-iterable-returning-itself/101595/29 "2025-08-10T18:07:20Z")

</div>

The closest I’m aware of is APL, but that’s a whole different matter, and it’s array-oriented in its design.

---

<div class="post-metadata">

**Author:** ![hginzel](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/hginzel/32/25510_2.png) [@hginzel](https://discuss.python.org/u/hginzel)\
**Post date:** [August 12, 2025, 12:24pm UTC](https://discuss.python.org/t/make-int-iterable-returning-itself/101595/30 "2025-08-12T12:24:56Z")

</div>

> [@tjreedy](#):
>
> But there is no reason to do that. If you want a single-item tuple, add the comma that makes one.

Why should I press the users of my function to distinguish in a call between one or more months? Why should I test it inside the function? See original example at the beginning.  
Still open question: What could happen when a scalar (int, float, complex, bool, NoneType) will be iterable returning itself?

---

<div class="post-metadata">

**Author:** ![peterc](https://avatars.discourse-cdn.com/v4/letter/p/f9ae1b/32.png) [@peterc](https://discuss.python.org/u/peterc)\
**Post date:** [August 12, 2025, 12:38pm UTC](https://discuss.python.org/t/make-int-iterable-returning-itself/101595/31 "2025-08-12T12:38:18Z")

</div>

> [@hginzel](#):
>
> Why should I press the users of my function to distinguish in a call between one or more months?

Perhaps you shouldn’t.

```python
def f(*months): ...

```

allows the user to call `f()` or `f(“jan”)` or `f(1,2,3)`. And the neat thing is that `months` is here always a tuple.

In Python characters are strings that you can iterate over. In Matlab, integers are also arrays that you can iterate over. It causes problems.

It is good for your code to crash early if you’re doing something wrong. If you think something is a tuple, but it is actually an int, there’s a flaw in your mental model of your code, and a TypeError is nice way to find out about that quickly.

If you’ve never had a bug propagate because you’re able to iterate over characters, good for you. But if you write enough code it’s eventually going to happen. When it does, you’ll understand why we don’t want the same potential to exist for scalars and None.

---

<div class="post-metadata">

**Author:** ![beauxq](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/beauxq/32/15042_2.png) [@beauxq](https://discuss.python.org/u/beauxq)\
**Post date:** [August 12, 2025, 3:32pm UTC](https://discuss.python.org/t/make-int-iterable-returning-itself/101595/32 "2025-08-12T15:32:26Z")

</div>

Your question of “what could happen” has already been answered, but I’ll restate it more explicitly:

```python
def foo(x: Iterable[int]) -> None:
    ...

```

In the current situation, if someone accidentally calls this with argument `5`, when they meant `range(5)`, static analysis can tell them that it’s a problem without even running the code.

With the change you’re suggesting, static analysis doesn’t tell them it’s a problem, and _what could happen_ is hours of frustrated debugging, rather than something that could have been found easily as soon as it was typed.

[Previous page](https://discuss.python.org/t/make-int-iterable-returning-itself/101595.md?page=1)
