How would I create a custom list class in python?

后端 未结 5 1371
一个人的身影
一个人的身影 2020-12-14 16:25

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:
           


        
5条回答
  •  一整个雨季
    2020-12-14 16:47

    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

提交回复
热议问题