UserList from module collections

ast none at gmail.com
Tue Sep 10 04:00:34 EDT 2019


Hello

I read in a course that class UserList from module
collections can be used to create our own custom list

Example

 >>> from collections import UserList
 >>> class MyList(UserList):
...     def head(self):
...         return self.data[0]
...     def queue(self):
...         return self.data[1:]
...
 >>> l = MyList([1, 2, 3])
 >>> l.append(4)
 >>> l
[1, 2, 3, 4]
 >>> l.head()
1
 >>> l.queue()
[2, 3, 4]


But I can do exactly the same without UserList


 >>> class MyList(list):
	def head(self):
	    return self[0]
	def queue(self):
	    return self[1:]

 >>> l = MyList([1, 2, 3])
 >>>  l.append(4)
 >>> l
[1, 2, 3, 4]
 >>> l.head()
1
 >>> l.queue()
[2, 3, 4]


So what UserList is used for ?



More information about the Python-list mailing list