从数组继承 [英] Inherit from array

查看:67
本文介绍了从数组继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好。


我正在尝试创建一个名为Vector的简单类,它继承自

数组。


class Vector(数组):

def __init __(自我,长度):

"""初始化大小为length的随机浮点数向量。浮点数

在区间[0; 1]"""

array .__ init __(self,''f'')

for _ in xrange(length):

self.apprend(random())


但是:

v =向量(10)



TypeError:array()参数1必须为char ,不是int


嗯,我猜这意味着数组的'__init__方法没有被调用

正确的参数......似乎有一个问题使用__init__

重载,就像我调用Vector(x)一样,它直接从数组调用__init__

方法,而不是在Vector类中定义的方法。任何人

对此有所了解?

解决方案

TG写道:

您好。

我正在尝试创建一个名为Vector的简单类,它继承自
数组。




其中数组?


bruno @ bousin~


python

Python 2.4.2(#1,2006年2月9日, 02:40:32)

[GCC 3.4.5(Gentoo 3.4.5,ssp-3.4.5-1.0,pie-8.7.9)] on linux2

输入帮助,版权,信用等。或许可证或更多信息。

array



Traceback (最近一次调用最后一次):

文件"< stdin>",第1行,在?

NameError:name''array''未定义< br $> b $ b -

bruno desthuilliers

python -c" print''@''。join([''。''。join( [w [:: - 1] for p in p.split(''。'')])for

p in''o **** @ xiludom.gro''。split('数组导入数组中的'@'')])"




类Vector(数组):

def __init __(self,size):

print" pouet"

array .__ init __(''f'')

print" pouet


v = Vector(''c'')

print repr(v)


将输出:


pouet

pouet

array(''c'')


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 ?

解决方案

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


这篇关于从数组继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆