I come from a Matlab background. In matlab I can create a class definition and then create an array of objects. I can easily dereference each object with an index. In addition, when I call a method from the object array (without an index), I have access to all the objects in the array. E.g, say that myNewClass has the properties .data and .text, it also has the method .clob. I can:
% init
a(1) = myNewClass;
a(2) = myNewClass;
a(1).data = [0;0;0];
a(2).data = [1;1;1];
So that now, if I call a.clob (not a(1).clob or a(2).clob), I can do something like
% this is a function inside the methods definition of my class definition
function clob(self)
% self here is my object array, "a", from above
for i=1:length(self)
% deref a single object
self(i).goClobYourself;
end
end
How do I do something like this in Python? Please note, I want my class to be an indexed collection, kind of like a list. But, I don't want my "class-list" to accept any class, just myNewClass. If I inherit from "list," will my class just be a "list" class with the attributes .data, .text, and the function .clob? Also note, I don't want a "list" that contains my objects, I want my objects to be list so that I can deref them from an index: a[1].clob() or a(1).clob() (?? or something like that). Or, I want to pass the whole array to self: a.clob() gives me access to the list. I may have the terminology a little cloudy.
Best regards
All such capabilities in Python use "Special method names" as described in the Language Reference section 3.3. Section 3.3.6 describes how to emulate container types, which in general is what you're asking for here. You need to define and implement methods __getitem__
, __setitem__
,__delitem__
and perhaps also __iter__
, __reversed__
and __contains__
. Python is quite good for this and the approach is very flexible.
When programming in Python, performing type checks isn't incredibly common. Are you positive you need your list to only accept one type (and its subtypes), or will you trust the programmer to read your docs and not put something in that's not supposed to be there?
If so, here's an example generic class that inherits from collections.MutableSequence
that would probably do what you want:
from collections import MutableSequence
class VerifierList(MutableSequence):
_list = None
def __init__(self, allowedClasses, *args, **kwargs):
super(VerifierList, self).__init__()
self._list = list(*args, **kwargs)
self.allowedClasses = tuple(allowedClasses)
def __repr__(self):
return repr(self._list)
def __str__(self):
return str(self._list)
def __len__(self):
return len(self._list)
def __getitem__(self, index):
return self._list[index]
def __setitem__(self, index, value):
if not isinstance(value, self.allowedClasses):
raise TypeError('Value of type %s not allowed!' % value.__class__.__name__)
self._list[index] = value
def __delitem__(self, index):
del self._list[index]
def insert(self, index, value):
if not isinstance(value, self.allowedClasses):
raise TypeError('Value of type %s not allowed!' % value.__class__.__name__)
self._list.insert(index, value)
Use it as follows:
>>> class A(object): pass
>>> class B(object): pass
>>> l = VerifierList((A,))
>>> l.append(A())
>>> print(l)
>>> [<__main__.A object at 0x000000000311F278>]
>>> l.append(B())
Traceback (most recent call last):
File "<pyshell#228>", line 1, in <module>
l.append(B())
File "C:\Python27\lib\_abcoll.py", line 661, in append
self.insert(len(self), value)
File "<pyshell#204>", line 23, in insert
raise TypeError('Value of type %s not allowed!' % value.__class__.__name__)
TypeError: Value of type B not allowed!