470,811 Members | 1,160 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,811 developers. It's quick & easy.

subclassing list

Hi,

I want to subclass "list". The documentation states to prefer subclassing
list instead of UserList. How to you clear the contents of a list subclass
without creating a new object?

Thanks in advance
Uwe
Jul 18 '05 #1
2 1760
class newList(list):
def clear(self):
self[:] = []

is one way.
HTH

Regards,

Fuzzy
http://www.voidspace.org.uk/python/index.shtml

Jul 18 '05 #2

[Uwe]
How [do] you clear the contents of a list subclass
without creating a new object?


Use the paranoia emoticon: "del x[:]". For example:
class L(list): .... pass
.... x = L()
x.append("Spam")
del x[:]
x [] type(x) <class '__main__.L'>


with-thanks-to-Gordon-McMillan-ly y'rs,

--
Richie Hindle
ri****@entrian.com

Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by GrelEns | last post: by
13 posts views Thread by Chris Cioffi | last post: by
3 posts views Thread by Emiliano Molina | last post: by
2 posts views Thread by BJörn Lindqvist | last post: by
12 posts views Thread by Jane Austine | last post: by
5 posts views Thread by Pieter Linden | last post: by
4 posts views Thread by David Coffin | last post: by
3 posts views Thread by jackstah | last post: by
5 posts views Thread by Mike Kent | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.