Should I return iterators or more sophisticated objects?

Posted by Erik on Programmers See other posts from Programmers or by Erik
Published on 2012-05-27T21:07:09Z Indexed on 2012/06/04 4:47 UTC
Read the original article Hit count: 283

Filed under:
|

Say I have a function that creates a list of objects. If I want to return an iterator, I'll have to return iter(a_list). Should I do this, or just return the list as it is? My motivation for returning an iterator is that this would keep the interface smaller -- what kind of container I create to collect the objects is essentially an implementation detail

On the other hand, it would be wasteful if the user of my function may have to recreate the same container from the iterator which would be bad for performance.

© Programmers or respective owner

Related posts about python

Related posts about coding-style