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:Use it as follows: