I have a container object like this:
class myclass():
self.data = dict()
self.otherattribute = 0 ## anything
how to write __iter__
and __next__
to implement:
for i in myclass:
equal to:
for i in myclass.data:
I have a container object like this:
class myclass():
self.data = dict()
self.otherattribute = 0 ## anything
how to write __iter__
and __next__
to implement:
for i in myclass:
equal to:
for i in myclass.data:
You can just have __iter__
return an iterator:
class myclass(object):
def __init__:
self.data = {}
def __iter__(self):
return iter(self.data)
With that, I can do this:
c = myclass()
c.data['key1'] = 'val1'
c.data['key2'] = 'val2'
for k in c:
print k
Which gets me:
key2
key1