473,396 Members | 1,840 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Intercepting Python list.append, list.remove

jonthysell
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
Sep 8 '08 #1
2 4410
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.
Sep 8 '08 #2
Laharl
849 Expert 512MB
You can override __delitem__(x) for del list[x], __delattr__(name) for del list.name, or __del__slice__ for deleting slices.
Sep 9 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

25
by: mike420 | last post by:
Ladies and Gentlemen, I present to you the final and ultimate proof of Python's brain-damage: As you may remember, flist = for i in range(3)
5
by: Ian McConnell | last post by:
What's the pythonic way of sending out a set of requests in parallel? My program throws an image at the server and then waits for the result. I'm currently using this bit of socket code to send...
7
by: svilen | last post by:
hello again. i'm now into using python instead of another language(s) for describing structures of data, including names, structure, type-checks, conversions, value-validations, metadata etc....
0
by: Volker Gietz | last post by:
Hello all, I have been using Python for almost a year now and by now I do 95% of my programming in it except some DSP stuff in C++. I learnt Python's basics in one week, yet the language still...
5
by: Michael Sperlle | last post by:
Is it possible? Bestcrypt can supposedly be set up on linux, but it seems to need changes to the kernel before it can be installed, and I have no intention of going through whatever hell that would...
0
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 349 open ( +7) / 3737 closed (+25) / 4086 total (+32) Bugs : 939 open (-12) / 6648 closed (+60) / 7587 total (+48) RFE : 249 open...
5
by: Ross | last post by:
Forgive my newbieness - I want to refer to some variables and indirectly alter them. Not sure if this is as easy in Python as it is in C. Say I have three vars: oats, corn, barley I add them...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
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,...
0
jinu1996
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.