I would like to write a custom list class in Python (let\'s call it MyCollection
) where I can eventually call:
for x in myCollectionInstance:
In Python 3 we have beautiful collections.UserList([list])
:
Class that simulates a list. The instance’s contents are kept in a regular list, which is accessible via the data attribute of UserList instances. The instance’s contents are initially set to a copy of list, defaulting to the empty list
[]
. list can be any iterable, for example a real Python list or a UserList object.In addition to supporting the methods and operations of mutable sequences, UserList instances provide the following attribute:
data
A real list object used to store the contents of the UserList class.
https://docs.python.org/3/library/collections.html#userlist-objects