470,638 Members | 1,642 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,638 developers. It's quick & easy.

Inherit from array

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

Apr 26 '06 #1
11 2604
TG wrote:
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.
array

Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'array' is not defined
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Apr 26 '06 #2
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')

Apr 26 '06 #3
I think he did

from array import *
Philippe


bruno at modulix wrote:
TG wrote:
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.
array

Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'array' is not defined
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"


Apr 26 '06 #4
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')
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
.... But here i'm getting lost.

Apr 26 '06 #5
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 'o****@xiludom.gro'.split('@')])"
Apr 26 '06 #6
Philippe Martin wrote:

bruno at modulix wrote:
TG wrote:
Hi there.

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


Which array ?


I think he did

from array import *


oops ! Sorry, I forgot this was in the standard lib (well, I never used
this module, so....)

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Apr 26 '06 #7
TG <gi****@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? 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?

--
\S -- si***@chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/
___ | "Frankly I have no feelings towards penguins one way or the other"
\X/ | -- Arthur C. Clarke
her nu becomež se bera eadward ofdun hlęddre heafdes bęce bump bump bump
Apr 26 '06 #8
Sion Arrowsmith wrote:
TG <gi****@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 'o****@xiludom.gro'.split('@')])"
Apr 26 '06 #9
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 ...

Apr 27 '06 #10
TG wrote:
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 ?
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Apr 27 '06 #11
bruno at modulix wrote:
TG wrote:
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.

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__.
--
-Scott David Daniels
sc***********@acm.org
Apr 27 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Fabian Neumann | last post: by
1 post views Thread by Jeff Schmidt | last post: by
7 posts views Thread by bobsled | last post: by
4 posts views Thread by Slavyan | last post: by
6 posts views Thread by Mohammad-Reza | last post: by
2 posts views Thread by ad | last post: by
19 posts views Thread by zzw8206262001 | last post: by
1 post views Thread by Korara | last post: by
???
1 post views Thread by Stoney L | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.