I’m implementing what is essentially a container object (although it does have a little of it’s own logic). I want to be able to iterate over items in a field in this class (which is just a plain list). Should I re-implement __iter__ and next for my class or is it acceptable to return the iterator of the list, like so:
class X:
def __init__(self):
self.list = []
def __iter__(self):
return self.list.__iter__()
I’m a little unsure if this will lead to any undesirable behavior.
It is fine to use the iterator of the built-in
listtype. I’d suggest not to call__iter__()explicitly, though, but rather use the built-in functioniter():Another option might be to derive
Xfromlist. (In Python 2.x, you should at least derive fromobject.)