From a conceptual perspective, the answer by @KonradRudolph is the right one.
But from a technical perspective, what makes an object able to be iterated over? This property is called being iterable. And in Python, an object is iterable as long as it implements the magic method __iter__
. Magic methods are special methods Python looks for in a class if it wants to use certain built-in behavior. Like iterating over an object, or adding two objects with +
.
And so, we could easily implement our own number class that is iterable:
class IterableInt:
def __init__(self, i):
self.i = i
def __iter__(self):
return map(int, str(self.i))
Here I chose iterable to mean what you expected, i.e. iterating over the decimal digits. Iterating over the binary digits instead would only mean changing one line.
Note that this is not really a number (yet), you cannot add or multiply it. For that one would probably want to inherit from int
, but that complicates things a bit more and is left as an exercise.
In any case, now we have an iterable integer:
n = IterableInt(123)
print(3 in n)
# True
print(list(n))
# [1, 2, 3]
If you need to find out if an object is iterable, there are two ways. One is called "Look before you leap" (LBYL), you can test for the existence of the __iter__
method or use the typing
module:
hasattr(n, "__iter__")
# True
hasattr(123, "__iter__")
# False
from typing import Iterable
isinstance(n, Iterable)
# True
isinstance(123, Iterable)
# False
And the other is "it’s easier to ask for forgiveness than permission" (EAFP), where you just assume the object is iterable and deal with it if it is not:
try:
3 in 123
except TypeError:
print("too bad, should have used IterableInt")