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 ? 11 2844
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('@')])"
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')
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('@')])"
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.
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('@')])"
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('@')])"
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
Sion Arrowsmith wrote: TG <gi****@gmail.com> wrote [something like]:from array import arrayclass 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('@')])"
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 ...
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('@')])"
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Fabian Neumann |
last post by:
Hi!
I got a problem with font-family inheritance. Let's say I have CSS
definitions like:
p {
font:normal 10pt Verdana;
}
strong {
font:normal 14pt inherit;
|
by: Jeff Schmidt |
last post by:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hello,
~ Ok, I've got some tables, and a style sheet that contains stuff to
format the table. The problem I'm having is, it appears that using...
|
by: bobsled |
last post by:
For class A to reuse functionality of a class B, A could either contains B
or inherits from B. When should pick which?
In a book the author says that "Don't inherit from a concrete class." Does...
|
by: Slavyan |
last post by:
(I just started to learn C#.NET)
What's the syntax in c# for a class to inherit more than one class.
I know following syntax:
public class MyClass : MyOtherClass
{
}
but I need to inherit...
|
by: Mohammad-Reza |
last post by:
I wrote a component using class library wizard. In my component i want to in
order to RightToLeft property do some works.
I can find out if user set this property to Yes or No, But if He/She set it...
|
by: ad |
last post by:
We can inherit a class.
But if the class is the code behind, when we inherit it , can we inherit
it's aspx ahead together ?
|
by: J |
last post by:
I tried to inherit 'Shot' class from 'Image' class, only to fail.
It gives me the CS0122 error, which says that it can't access
'System.Drawing.Image.Image()'.
What am I missing?
using...
|
by: zzw8206262001 |
last post by:
Hi,I find a way to make javescript more like c++ or pyhon
There is the sample code:
function Father(self) //every contructor may have "self"
argument
{
self=self?self:this; ...
|
by: GTalbot |
last post by:
Hello fellow comp.infosystems.www.authoring.stylesheets colleagues,
Imagine this situation:
#grand-parent-abs-pos
{
height: 400px;
position: absolute;
width: 600px;
}
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |