Help | Site Map
Connecting Tech Pros Worldwide
Reply
 
LinkBack Thread Tools
  #1  
Old September 8th, 2008, 11:28 PM
jonthysell's Avatar
Newbie
 
Join Date: Sep 2008
Location: Redmond, WA
Age: 24
Posts: 4
Default Intercepting Python list.append, list.remove

I have a list as an attribute of a class. I need to be able to intercept any add/remove of objects to the list, so that I can perform cleanup on other attributes of the class.

Currently, I'm making the list "private" with the horrid __, and then having a slew of accessor functions to iterate, add/remove, etc. without giving true access to the list. As such the class seems kludgey.

Is there a more pythonic way of doing this? Possibly by sub-classing list? How will this effect the performance of list operations?

Every time I google this all I get is results about python mailing lists.

Thanks,

/jon
Reply
  #2  
Old September 9th, 2008, 12:25 AM
jonthysell's Avatar
Newbie
 
Join Date: Sep 2008
Location: Redmond, WA
Age: 24
Posts: 4
Default

Ok, seems I can just sub-class list. I can live with another class I suppose (better for organization I suppose).

Is there anyway to override the del keyword? So that when I do del mylistsubclassinstance[index] I can perform cleanup?

And again, would a list sub-class perform slower on operations that I do/don't override? I'm not too worried about performance at this point, but it's for a simulation engine, which might mean long running processes, so shaving a few hours can be beneficial.
Reply
  #3  
Old September 9th, 2008, 04:29 AM
Expert
 
Join Date: Sep 2007
Posts: 846
Default

You can override __delitem__(x) for del list[x], __delattr__(name) for del list.name, or __del__slice__ for deleting slices.
Reply
Reply

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles