clearing subclassed lists/dicts

Gary Herron gherron at islandtraining.com
Thu May 30 15:42:35 EDT 2002


On Thursday 30 May 2002 11:38 am, g2 at iowegian.com wrote:
> Hi All,
>
> I've gotten interested in subclassing lists and dictionaries, and that got
> me wondering if there's an elegant way to clear the contents of the
> underlying type.
>
> I tried a couple of things that I didn't really expect to work:

Use 
 del self[:]
in the clear method:

>>> class L(list):
...   def clear(self):
...     del self[:]
...
>>> l = L()
>>> l.append(1)
>>> l
[1]
>>> l.clear()
>>> l
[]
>>>





Gary Herron






More information about the Python-list mailing list