[Tutor] subscript problem
Alan Gauld
alan.gauld@blueyonder.co.uk
Tue Jun 3 18:11:02 2003
> class AB:
> def __init__(self):
> self.n_p = None
> self.po = Vec() # in c struct this is Vec* po
> self.con = [] # in c struct this is short* con
>
>
> class Vec:...
> def XYZ(out):
> n = 0
> out.con.append(5)
> last = out.con[n]
So last now equals 5 after the first call.
> v_start = out.po[last]
But out is a Vec object and a Vec is not a sequence
so you can't index into it! You could if you defined a __getItem__
method which is how to override the []
operator in Python. That's why the error message says
what it does. You are trying to index into an object
which is not a sequence and doesn't provide a getItem
method.
Even if you created a list of Vec() objects you would
still get an IndexError if there were less than 5 of them.
Can you explain in words what you are trying to do with
Vec, AB etc?
Maybe we can find a more Pythonic way of doing it?
Alan G.