Reputation: 2302
I want to iterate through all objects in an array of URL objects I've got
class Url(object):
pass
a = Url()
a.url = 'http://www.heroku.com'
a.result = 0
b = Url()
b.url = 'http://www.google.com'
b.result = 0
c = Url()
c.url = 'http://www.wordpress.com'
c.result = 0
urls = [a, b, c]
for i, u in urls:
print(i)
print(u)
However, when I run this script, it comes back with the following error:
TypeError: cannot unpack non-iterable Url object
How do I fix this?
Upvotes: 5
Views: 50525
Reputation: 6748
Try this:
class Url(object):
pass
a = Url()
a.url = 'http://www.heroku.com'
a.result = 0
b = Url()
b.url = 'http://www.google.com'
b.result = 0
c = Url()
c.url = 'http://www.wordpress.com'
c.result = 0
urls = [a, b, c]
for i in urls:
print(i)
To iterate though the urls. To get the result and urls (which I think you are trying to do), do this:
class Url(object):
pass
a = Url()
a.url = 'http://www.heroku.com'
a.result = 0
b = Url()
b.url = 'http://www.google.com'
b.result = 0
c = Url()
c.url = 'http://www.wordpress.com'
c.result = 0
urls = [a, b, c]
for c,i in enumerate(urls):
print("index is ",c)
print(i.result)
print(i.url)
Upvotes: 7