Inherit from array

TG girodt at gmail.com
Wed Apr 26 09:15:15 EDT 2006


Hi there.

I'm trying to create a simple class called Vector which inherit from
array.

class Vector(array):
    def __init__(self,length):
        """initialize a vector of random floats of size length. floats
are in interval [0;1]"""
        array.__init__(self,'f')
        for _ in xrange(length):
            self.apprend(random())

but then :
>>> v = Vector(10)
TypeError: array() argument 1 must be char, not int

Well, I guess it means array's __init__ method is not called with
proper arguments ... It seems there is a problem with __init__
overloading, like when I call Vector(x), it directly calls __init__
method from array rather than the one defined in Vector class. Anyone
got an idea on this ?




More information about the Python-list mailing list