Inherit from array

bruno at modulix onurb at xiludom.gro
Wed Apr 26 12:39:36 EDT 2006


Sion Arrowsmith wrote:
> TG <girodt at gmail.com> wrote [something like]:
>>from array import array
> 
>>class Vector(array):
>>   def __init__(self,size):
>>       array.__init__('f')
>>
>>v = Vector('c')
>>print repr(v)
>>
>>will output :
>>
>>array('c')
> 
> 
> Is this a case of new-sytle classes being confusing? 

Nope. FWIW, array is coded in C, and seems not to follow all standard
conventions...

> Because
> I'm certainly confused. I guess what's needed is:
> 
> class Vector(array):
>     def __new__(cls, size):
>         self = array.__new__(array, 'f')
>         ...
>         return self

Yes.

> But how does one determine what classes need to have __init__
> overridden and which __new__ when subclassing?

It's the first exemple I see of a mutable type needing this.

NB :
http://www.python.org/doc/2.4.2/ref/customization.html
"""
__new__() is intended mainly to allow subclasses of immutable types
(like int, str, or tuple) to customize instance creation.
"""

Usually, overriding __init__ is the way to go.

-- 
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