'builtin_function_or_method' object is not iterable
Question:
I’m trying to print all cars in stuff from this json:
{
"stuff": [
{
"car" : 1,
"color" : "blue"
},
{
"bcarus" : 2,
"color" : "red"
}
],
}
In my Serializer I access the data like this….
stuff = self.context.get(“request”).data.stuff
But when I do the following…
for item in stuff:
print(item)
I get he error:
‘builtin_function_or_method’ object is not iterable
Why do I get this error?
How can I access stuff
in a for loop
?
When I do print(self.context.get("request").data.stuff)
I get <built-in method items of dict object at 0x105225050>
which I assumed print the stuff
instead.
Answers:
stuff
ends up a method of function so you would need to call it:
for item in stuff():
print(item)
Which based on your comment is the dict.items
.
So you can unpack:
for k,v in stuff():
print(k,v)
Or just call when you assign:
stuff = self.context.get("request").data.stuff()
for k,v in stuff:
print(k,v)
my_dict={"Car1":"Audi","Car2":"BMW","Car3":"Audi"}
for x in my_dict.values:
print(x)
‘builtin_function_or_method’ object is not iterable
This may be because my_dict.values
is a function that is expecting empty "()" or some value in it
I had the same issue but for me I had items as an object key which coincidentally references a python object method Object.items(), had to change the key name in my object
Hey guys I came here looking for a solution to the iteration problem and I actually happened to stumble on the solution after noticing smth missing in the codes try adding () to the method for example
my_dict= {"Car1":"Audi","Car2":"BMW","Car3":"Audi"}
for x in my_dict.values():
print(x)
I’m trying to print all cars in stuff from this json:
{
"stuff": [
{
"car" : 1,
"color" : "blue"
},
{
"bcarus" : 2,
"color" : "red"
}
],
}
In my Serializer I access the data like this….
stuff = self.context.get(“request”).data.stuff
But when I do the following…
for item in stuff:
print(item)
I get he error:
‘builtin_function_or_method’ object is not iterable
Why do I get this error?
How can I access stuff
in a for loop
?
When I do print(self.context.get("request").data.stuff)
I get <built-in method items of dict object at 0x105225050>
which I assumed print the stuff
instead.
stuff
ends up a method of function so you would need to call it:
for item in stuff():
print(item)
Which based on your comment is the dict.items
.
So you can unpack:
for k,v in stuff():
print(k,v)
Or just call when you assign:
stuff = self.context.get("request").data.stuff()
for k,v in stuff:
print(k,v)
my_dict={"Car1":"Audi","Car2":"BMW","Car3":"Audi"}
for x in my_dict.values:
print(x)
‘builtin_function_or_method’ object is not iterable
This may be because my_dict.values
is a function that is expecting empty "()" or some value in it
I had the same issue but for me I had items as an object key which coincidentally references a python object method Object.items(), had to change the key name in my object
Hey guys I came here looking for a solution to the iteration problem and I actually happened to stumble on the solution after noticing smth missing in the codes try adding () to the method for example
my_dict= {"Car1":"Audi","Car2":"BMW","Car3":"Audi"}
for x in my_dict.values():
print(x)