Inherit from array

bruno at modulix onurb at xiludom.gro
Wed Apr 26 11:03:05 EDT 2006


TG wrote:
> Obviously, there is something I didn't catch in python's inheritance.

Nope. Obviously, array.array doesn't respect the usual rules.

> from array import array
> class Vector(array):
>     def __init__(self,size):
>         print self.typecode
>         array.__init__(self,'f')
> 
> 
>>>>v = Vector('c')
> 
> c
> 
> Here, it says the typecode is 'c' - I thought such an information was
> initalized during the array.__init__(self,'f') but obviously I was
> wrong.
> 
> Maybe the typecode is defined before, during the call to __new__ method

I think this must be something along this line.

> ... But here i'm getting lost.
> 
Let's see :

from array import array

class Vector(array):
    def __new__(cls, size):
        v = super(Vector, cls).__new__(cls, 'f')
        #print "v is %s" % v
        return v
    def __init__(self, size):
        self.size = size

v = Vector(42)
print v


HTH
-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"



More information about the Python-list mailing list