having list attribute to track max size

Terry Reedy tjreedy at udel.edu
Sat Apr 5 03:31:57 CEST 2008

<ankitks.mital at gmail.com> wrote in message 
news:f4fe2704-92fd-4fee-a2a9-98d2edcc5a5d at o1g2000pra.googlegroups.com...
| Lets say I have a dynamic list class (may be extended from list),
| where I add and remove items during program.
| a = []
| a.append(1)
| ....
| I am trying to find is there easy way keep track of 'maximum size of
| list reached"
| so for example len(a) goes from 0->3->4->3
| If I call a.max_size_ever(), I will get 4

Here is a start:

>>> class mlist(list):
  def __init__(self,it):
    self.maxlen = len(self)

>>> ll = mlist((1,2,3))
>>> ll.maxlen

Now, add methods for the list grow methods (.append, .extend, and 
.__setslice__) which follow a call to the parent method with

self.maxlen = max(self.maxlen, len(self)) # or equivalent code


More information about the Python-list mailing list