473,724 Members | 2,222 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

subclassing list


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
Jul 18 '05 #1
2 1910
class newList(list):
def clear(self):
self[:] = []

is one way.



Jul 18 '05 #2

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

by: GrelEns | last post by:
hello, i wonder if this possible to subclass a list or a tuple and add more attributes ? also does someone have a link to how well define is own iterable object ? what i was expecting was something like : >>> t = Test('anAttributeValue', ) >>> t.anAttribute
by: Chris Cioffi | last post by:
Hello all! I'm trying to subclass int such that once it reaches a certain value, it flips back to the starting count. This is for use as an X12 control number. Subclassing int and getting the display like I want it was trivial, however what's the best way to make it such that: 990 + 1 = 991 991 + 1 = 992 .... 998 + 1 = 999
by: Emiliano Molina | last post by:
Where can I find information on how to do this? Specifically I am concerned about something like: class NewList(list): def __init__(self): list.__init__(self) The above code does not allow the passing of a sequence to populate the NewList.
by: BJörn Lindqvist | last post by:
A problem I have occured recently is that I want to subclass builtin types. Especially subclassing list is very troublesome to me. But I can't find the right syntax to use. Take for example this class which is supposed to be a representation of a genome: class Genome(list): def __init__(self): list.__init__(self) self = ....
by: Jane Austine | last post by:
Please see the following code: -------------------------------- class rev_wrap(object): def __init__(self,l): self.l=l def __getitem__(self,i): return self.l class rev_subclass(list): def __getitem__(self,i):
by: Pieter Linden | last post by:
Hi, This question refers sort of to Rebecca Riordan's article on Access web about subclassing entities: http://www.mvps.org/access/tables/tbl0013.htm How practical is this? I am writing a database to track Computer equipment/inventory as well as other devices. Computers, of course, have software installed, so I want to keep track of that. I was
by: David Coffin | last post by:
I'd like to subclass int to support list access, treating the integer as if it were a list of bits. Assigning bits to particular indices involves changing the value of the integer itself, but changing 'self' obviously just alters the value of that local variable. Is there some way for me to change the value of the BitSequence object itself? I've also tried wrapping and delegating using __getattr__, but I couldn't figure out how to handle...
by: jackstah | last post by:
I've read before that subclassing off of std::vector is a bad idea because it's a concrete class. I understand, but there shouldnt be any issues with this. I just wanted to do a simple example in my project to combine templates and inheritance so that I would brush up on both. I have data that is mostly sorted, but there might be some flipflops here and there. Thus I've subclassed off of vector, and written a class that, given a...
by: Mike Kent | last post by:
For Python 2.5 and new-style classes, what special method is called for mylist = seq and for del mylist (given that mylist is a list, and seq is some sequence)? I'm trying to subclass list, and I'm having trouble determining what special methods I have to override in my class for the above two operations. From my testing, it seems to be __setslice__ for both, but the docs say __setslice__ and brethren are deprecated. I would have...
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.