As far as I can tell, making a class object iterable by using a metaclass works just fine:

from __future__ import print_function

class IterableCar(type):
    def __iter__(cls):
        return iter(cls.__name__)

class Car(object):
    __metaclass__ = IterableCar

    def __init__(self, name):
        self.name = name


if __name__=='__main__':

    car1 = Car('Mercedes')
    car2 = Car('Toyota')
    for cars in Car:
        print (cars)

Results in:

mgilson$ python ~/sandbox/test.py 
C
a
r

Here's an example where I actually track the cars generated:

from __future__ import print_function
import weakref

class IterableCar(type):

    _cars = weakref.WeakSet()

    def __iter__(cls):
        return iter(cls._cars)

    def add_car(cls, car):
        cls._cars.add(car)


class Car(object):
    __metaclass__ = IterableCar

    def __init__(self, name):
        self.__class__.add_car(self)
        self.name = name


if __name__=='__main__':

    car1 = Car('Mercedes')
    car2 = Car('Toyota')
    for cars in Car:
        print (cars.name)

Note that if you're using python3.x, to use a metaclass you do:

class Car(metaclass=IterableCar):
    ...

Rather than:

class Car(object):
    __metaclass__ = IterableCar

which likely explains the problem that you're experiencing.

Answer from mgilson on Stack Overflow
🌐
Rollbar
rollbar.com › home › how to fix typeerror: int object is not iterable in python
How to Fix TypeError: Int Object Is Not Iterable in Python
File "test.py", line 3, in <module> for i in myint: TypeError: 'int' object is not iterable · In the above example, myint cannot be iterated over since it is an integer value. The Python range() function can be used here to get an iterable object that contains a sequence of numbers starting from 0 and stopping before the specified number. Updating the above example to use the range() function in the for loop fixes the error:
Published: 1 week ago
🌐
freeCodeCamp
freecodecamp.org › news › int-object-is-not-iterable-python-error-solved
Int Object is Not Iterable – Python Error [Solved]
March 24, 2022 - If you are running your Python code and you see the error “TypeError: 'int' object is not iterable”, it means you are trying to loop through an integer or other data type that loops cannot work on.
Discussions

python - TypeError: 'type' object is not iterable - Iterating over object instances - Stack Overflow
I am working on a project and I would like to make one of my classes iterable. To the best of my knowledge I can do that with using metaclass. First of all I would like to understand how metaclass... More on stackoverflow.com
🌐 stackoverflow.com
python - TypeError: 'type' object is not iterable in a for loop - Stack Overflow
I have looked at other threads and can't figure out what's wrong. All the answers are too complex for me to process well. I keep getting the following error: TypeError: 'type' object is not itera... More on stackoverflow.com
🌐 stackoverflow.com
January 3, 2020
"'Int' Object is not iterable" Can someone explain what I'm doing wrong?
There are unfortunately a large number of issues with this code. The one that's causing your immediate problem is that, as the error says, user_input is an integer, and you can't iterate over an integer. You mention range in the comment but you don't actually use it; you probably mean: for i in range(user_input): But I'm not sure what the point of the loop is, because you're not doing any of the other things that the instructions tell you to do. What are you planning to do in each iteration of the loop? More on reddit.com
🌐 r/learnpython
3
0
February 9, 2023
python saying object is not iterable
Do not use a for loop. Posting from mobile. But simpler would be Var1=random number While Var1 > 0 Print stuff Var1 = Var1 - 1 More on reddit.com
🌐 r/learnpython
9
1
October 1, 2022
People also ask

What is Python TypeError and what causes it?
TypeError is raised when an operation is applied to an object of the wrong type. Common patterns: calling a non-callable object, adding incompatible types (str + int), passing the wrong number of arguments, or accessing attributes on a NoneType. Each TypeError message names the operation and expected vs actual types, the fix is almost always to convert types explicitly (int(), str()) or fix the wrong variable assignment.
🌐
itsourcecode.com
itsourcecode.com › home › typeerror: ‘int’ object is not iterable
Fix Int() Object is Not Iterable (2026 Python)
How do I quickly debug a Python TypeError?
Three steps: (1) Read the full error message, it names the exact operation and types involved. (2) Print the type of every variable in that line: print(type(var1), type(var2)). (3) Check what the function expected vs what you passed. Most TypeError fixes are 1-line type casts or fixing a variable that became None unexpectedly.
🌐
itsourcecode.com
itsourcecode.com › home › typeerror: ‘int’ object is not iterable
Fix Int() Object is Not Iterable (2026 Python)
Should I catch TypeError or let it propagate?
For internal code, let TypeError propagate, it's almost always a real bug (wrong type passed). For boundary code (parsing user input, third-party API responses), catch TypeError + ValueError together: try: parsed = int(value) except (TypeError, ValueError): parsed = 0. Catching internal TypeErrors hides bugs.
🌐
itsourcecode.com
itsourcecode.com › home › typeerror: ‘int’ object is not iterable
Fix Int() Object is Not Iterable (2026 Python)
🌐
Reddit
reddit.com › r/learnpython › typeerror: 'type' object is not iterable
r/learnpython on Reddit: TypeError: 'type' object is not iterable
October 15, 2021 -

Can somebody help me fix the problem...I get the above error when I run the following chunk of code:

# Returns true if number is wholly divisible by divisor with no remainder.

def isDivisible(number, divisor):

return number % divisor == 0

def sequence(N):

#results = [item for item in range(1, N+1)]

results = []

for i in range(1, N+1):

results.append(i)

return type(results)

def HorseDuckProblem(sequence):

for item in sequence:

if isDivisible(item, 30):

sequence[item] = "HorseDuck"

elif isDivisible(item, 5):

sequence[item] = "Horse"

elif isDivisible(item, 6):

sequence[item] = 'Duck'

else:

sequence[item] = str(item)

return sequence

if __name__ == "__main__":

seq = sequence(100)

print(HorseDuckProblem(seq))

🌐
Career Karma
careerkarma.com › blog › python › python typeerror: ‘int’ object is not iterable solution
Python typeerror: ‘int’ object is not iterable Solution | Career Karma
December 1, 2023 - For instance, if you try to apply a mathematical function to a string, or call a value like a function which is not a function, a TypeError is raised. The error message tells us that you have tried to iterate over an object that is not iterable. ...
🌐
Python
bugs.python.org › issue32259
Issue 32259: Misleading "not iterable" Error Message when generator return a "simple" type, and a tuple is expected - Python tracker
December 9, 2017 - This issue tracker has been migrated to GitHub, and is currently read-only. For more information, see the GitHub FAQs in the Python's Developer Guide · This issue has been migrated to GitHub: https://github.com/python/cpython/issues/76440
Top answer
1 of 3
4

As far as I can tell, making a class object iterable by using a metaclass works just fine:

from __future__ import print_function

class IterableCar(type):
    def __iter__(cls):
        return iter(cls.__name__)

class Car(object):
    __metaclass__ = IterableCar

    def __init__(self, name):
        self.name = name


if __name__=='__main__':

    car1 = Car('Mercedes')
    car2 = Car('Toyota')
    for cars in Car:
        print (cars)

Results in:

mgilson$ python ~/sandbox/test.py 
C
a
r

Here's an example where I actually track the cars generated:

from __future__ import print_function
import weakref

class IterableCar(type):

    _cars = weakref.WeakSet()

    def __iter__(cls):
        return iter(cls._cars)

    def add_car(cls, car):
        cls._cars.add(car)


class Car(object):
    __metaclass__ = IterableCar

    def __init__(self, name):
        self.__class__.add_car(self)
        self.name = name


if __name__=='__main__':

    car1 = Car('Mercedes')
    car2 = Car('Toyota')
    for cars in Car:
        print (cars.name)

Note that if you're using python3.x, to use a metaclass you do:

class Car(metaclass=IterableCar):
    ...

Rather than:

class Car(object):
    __metaclass__ = IterableCar

which likely explains the problem that you're experiencing.

2 of 3
3

To track instances of the class that are created, we'll start by adding a _cars attribute to each the class created by the metaclass. This will be set of weak references, so that the class itself does not prevent unused instances from being garbage-collected.

class IterableCar(type):
    def __new__(meta, name, bases, attrs):
        attrs['_cars'] = weaker.WeakSet()
        return type.__new__(meta, name, bases, attrs)

To add the instances, we'll override __call__. Essentially, this is where you put code that you would ordinarily put in __new__ or __init__ when defining the class itself.

    def __call__(cls, *args, **kwargs):
        rv = type.__call__(cls, *args, **kwargs)
        cls._cars.add(rv)
        return rv

And to make the class iterable by iterating over its set of instances,

   def __iter__(self):
       return iter(self._cars)

Any class using IterableCar will automatically track its instances.

class Car(metaclass=IterableCar):
    def __init__(self, name):
        self.name = name

car1 = Car('Mercedes')
car2 = Car('Toyota')
for cars in Car:
    print(cars.name)
Find elsewhere
🌐
Itsourcecode
itsourcecode.com › home › typeerror: ‘int’ object is not iterable
Fix Int() Object is Not Iterable (2026 Python)
July 12, 2026 - It can be fixed by either Using a range() function, Converting the integer into a string and Passing an iterable object such as a list, tuple, or string. ... In Python, iteration is the process of looping over each item in a collection, such ...
🌐
HackerNoon
hackernoon.com › how-to-fix-the-python-typeerror-int-object-is-not-tterable
How to fix the Python TypeError: ‘int’ Object is not Iterable | HackerNoon
September 9, 2021 - In Python, unlike lists, integers are not directly iterable as they hold a single integer value and do not contain the **‘__iter__‘ ** method; that’s why you get a TypeError. You can run the below command to check whether an object is iterable or not. ... There are two ways you can resolve the issue, and the first approach is instead of using int, try using a list if it makes sense, and it can be iterated using for and while loop ...
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-fix-typeerror-nonetype-object-is-not-iterable-in-python
How to Fix TypeError: 'NoneType' object is not iterable in Python - GeeksforGeeks
April 28, 2025 - The error message "TypeError: 'NoneType' object is not iterable" in Python typically occurs when you try to iterate over an object that has a value of None. This error is raised because None is not iterable, meaning you cannot loop through it ...
🌐
Finxter
blog.finxter.com › home › learn python blog › how to solve python “typeerror: ‘int’ object is not iterable”?
How to Solve Python "TypeError: ‘int’ object is not iterable"? - Be on the Right Side of Change
September 17, 2020 - Thus, the ‘for’ construct in Python expects an iterable object which to be traversed, and cannot interpret an integer. This error can be easily corrected using the function ‘range’. Let’s see how our example would look in this case. ... The ‘range’ function can take 3 arguments like this: range(start, stop[, step]). The ‘start’ is the first number from which the loop will begin, ‘stop’ is the number at which the loop will end.
🌐
Medium
medium.com › @rick.wayne.2022 › how-to-fix-the-python-typeerror-int-object-is-not-iterable-30b960c69901
How to Fix the Python TypeError: ‘int’ Object is not Iterable | by Rick Wayne | Medium
January 24, 2023 - This error occurs because the int type in Python is not iterable. In other words, you cannot use a for loop to iterate over an integer object in Python.
🌐
Python Pool
pythonpool.com › home › error › fix typeerror: int object is not iterable in python
[Solved] TypeError: 'int' Object is Not Iterable - Python Pool
July 13, 2026 - Quick answer: TypeError: ‘int’ object is not iterable means Python expected an object that can produce items one by one but received a single integer. Use range(count) when the integer is a repetition count, use a list or tuple when it ...
🌐
PyTutorial
pytutorial.com › fix-typeerror-type-object-is-not-iterable
PyTutorial | Fix TypeError: 'type' object is not iterable
April 9, 2025 - Traceback (most recent call last): ... with ModuleNotFoundError when imports go wrong. The solution is to ensure you're working with objects, not types....
🌐
TechGeekBuzz
techgeekbuzz.com › blog › python-typeerror
What is the ‘int’ object is not iterable Python Typeerror?
The 'int' object is not iterable is a Python typeerror that results due to incorrect use of the for loop. Know how to fix it here. Read More »
🌐
Bomberbot
bomberbot.com › python › int-object-is-not-iterable-python-error-solved
Int Object is Not Iterable – Python Error [Solved] - Bomberbot
April 21, 2024 - Traceback (most recent call last): File "main.py", line 4, in <module> for num in start, stop: TypeError: ‘int‘ object is not iterable · Why? Because start and stop are just two int objects – (5, 15) creates a tuple, not a range object. The correct syntax would be: ... Another potential pitfall is trying to unpack a dictionary directly in a for loop without specifying which part to iterate over (.keys(), .values() or .items()).
🌐
CMARIX
cmarix.com › home › how to fix typeerror: ‘int’ object is not iterable in python?
Fix TypeError: 'int' Object is Not Iterable in Python
February 5, 2026 - So when you write something like: ... The ‘int’ object is not iterable error usually means there’s a mix-up in data types. Use str() to loop over digits, range() for numeric loops, and list multiplication for repetition.
🌐
Arrowhitech
blog.arrowhitech.com › typeerror-nonetype-object-is-not-iterable
Typeerror: ‘nonetype’ object is not iterable: How to solve this error in python – Blogs | AHT Tech | Digital Commerce Experience Company
Above all, to solve this error: Python nonetype object is not iterable, make sure that any values that you try to iterate over have been assigned an iterable object, like a string or a list. To clarify, in our example, we forgot to add a “return” statement to a function.
🌐
CodeFatherTech
codefather.tech › home › blog › python typeerror: int object is not iterable: what to do to fix it?
Python TypeError: int object is not iterable: What To Do To Fix It?
December 8, 2024 - Traceback (most recent call last): ... iterable ... The cause of this error is the fact that we are using a for loop to iterate through the variable with value len(numbers) and this variable is an integer that contains the number of elements in the list numbers....