I want a python list which represents itself externally as an average of its internal list items, but otherwise behaves as a list. It should raise a TypeError
if an item is added that can't be cast to a float.
The part I'm stuck on is raising TypeError
. It should be raised for invalid items added via any list method, like .append
, .extend
, +=
, setting by slice, etc.
Is there a way to intercept new items added to the list and validate them?
I tried re-validating the whole list in __getattribute__
, but when its called I only have access to the old version of the list, plus it doesn't even get called initialization, operators like +=
, or for slices like mylist[0] = 5
.
Any ideas?
The
array.array
class will take care of thefloat
part:And some tests:
Here's how to create a subclass using the
MutableSequence
abstract base class in thecollections
module as its base class (not fully tested -- an exercise for the reader ;-):The general approach would be to create your own class inheriting vom list and overwriting the specific methods like append, extend etc. This will probably also include magic methods of the Python list (see this article for details: http://www.rafekettler.com/magicmethods.html#sequence).
For validation, you will need to overwrite __setitem__(self, key, value)
There are 7 methods of the
list
class that add elements to the list and would have to be checked. Here's one compact implementation:Actually the best answer may be: don't.
Checking all objects as they get added to the list will be computationally expensive. What do you gain by doing those checks? It seems to me that you gain very little, and I'd recommend against implementing it.
Python doesn't check types, and so trying to have a little bit of type checking for one object really doesn't make a lot of sense.
Inherit from MutableSequence and implement the methods it requires as well as any others that fall outside of the scope of Sequences alone -- like the operators here. This will allow you to change the operator manipulations for list-like capabilities while automatically generating iterators and contains capabilities.
If you want to check for slices btw you need to do
isinstance(key, slice)
in your__getitem__
(and/or__setitem__
) methods. Note that a single index like myList[0] is not a slice request, but a single index and myList[:0] is an actual slice request.