I have written a custom container object.
According to this page, I need to implement this method on my object:
However, upon following up the link to Iterator Types in the Python reference manual, there are no examples given of how to implement your own.
Can someone post a snippet (or link to a resource), that shows how to do this?
The container I am writing, is a map (i.e. stores values by unique keys). dicts can be iterated like this:
for k, v in mydict.items()
In this case I need to be able to return two elements (a tuple?) in the iterator. It is still not clear how to implement such an iterator (despite the several answers that have been kindly provided). Could someone please shed some more light on how to implement an iterator for a map-like container object? (i.e. a custom class that acts like a dict)?
I normally would use a generator function. Each time you use a yield statement, it will add an item to the sequence.
The following will create an iterator that yields five, and then every item in some_list.
def __iter__(self): yield 5 yield from some_list
yield from didn"t exist, so you would have to do:
def __iter__(self): yield 5 for x in some_list: yield x
Managing and analyzing data have always offered the greatest benefits and the greatest challenges for organizations of all sizes and across all industries. Businesses have long struggled with finding ...
This book introduces machine learning methods in finance. It features a unified treatment of machine learn...
Professional-quality code does more than just run without bugs. It’s clean, readable, and easy to maintain. To step up from a capable Python coder to a professional developer, you need to learn indu...
We live in an age of so-called Big Data. We hear terms like data scientist, and there is much talk about analytics and the mining of large amounts of corporate data for tidbits of business value. Ther...