Inherit from array

T

TG

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 :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 ?
 
B

bruno at modulix

TG said:
Hi there.

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

Which array ?

bruno@bousin ~ $ python
Python 2.4.2 (#1, Feb 9 2006, 02:40:32)
[GCC 3.4.5 (Gentoo 3.4.5, ssp-3.4.5-1.0, pie-8.7.9)] on linux2
Type "help", "copyright", "credits" or "license" for more information.Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'array' is not defined
 
T

TG

from array import array
class Vector(array):
def __init__(self,size):
print "pouet"
array.__init__('f')
print "pouet"

v = Vector('c')
print repr(v)

will output :

pouet
pouet
array('c')
 
P

Philippe Martin

I think he did

from array import *


Philippe



TG said:
Hi there.

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

Which array ?

bruno@bousin ~ $ python
Python 2.4.2 (#1, Feb 9 2006, 02:40:32)
[GCC 3.4.5 (Gentoo 3.4.5, ssp-3.4.5-1.0, pie-8.7.9)] on linux2
Type "help", "copyright", "credits" or "license" for more information.Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'array' is not defined
 
T

TG

Obviously, there is something I didn't catch in python's inheritance.

from array import array
class Vector(array):
def __init__(self,size):
print self.typecode
array.__init__(self,'f')
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
.... But here i'm getting lost.
 
B

bruno at modulix

TG said:
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')



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
 
S

Sion Arrowsmith

TG said:
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? 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

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

bruno at modulix

Sion said:
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.
 
T

TG

Hmm ... I'm definitely not a python wizard, but it seems to be quite a
special case that breaks the rules ... unpythonic, isn't it ?

Has anyone seen a PEP on this subject ?

Just in case a troll reads this message : i'm not saying python sucks
or has huge design flaws here ...
 
B

bruno at modulix

TG said:
Hmm ... I'm definitely not a python wizard, but it seems to be quite a
special case that breaks the rules ...

Yes and no. The primary use case for __new__ was to allow subclassing of
immutable types. array.array is not immutable, but it's still a special
case, in that it enforce type-based restrictions.

I guess that it does so by creating different types based on the
typecode (this is low-level, C stuff), so this can only happen in the
constructor (the object is already created when the initializer is called).
unpythonic, isn't it ?

Not really -> practicallity beats purity !-)

But this should definitively be documented. I don't have time to do so
right now - anybody willing to take care of this ?
 
S

Scott David Daniels

bruno said:
Yes and no. The primary use case for __new__ was to allow subclassing of
immutable types. array.array is not immutable, but it's still a special
case, in that it enforce type-based restrictions.
The alternative is to either:
1) Allow arrays that have no type.
or 2) Allow arrays that change type.
Neither is a tasty alternative. __new__ sets the stuff that
must be invariant (and for an array, that is the element type),
and __init__ does any further initialization (initialization
should be re-runnable). In the case of array, filling the array
with data seems a great use of __init__.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top