I want to get the vector like: v:[1.0, 2.0, 3.0]
我想要得到向量v:[1.0, 2.0, 3.0]
Here is my code:
这是我的代码:
class VECTOR(list) :
def _init_ (self,x=0.0,y=0.0,z=0.0,vec=[]) :
list._init_(self,[float(x),float(y),float(z)])
if vec :
for i in [0,1,2] :
self[i] = vec[i]
But when I typed: a = VECTOR(1,2,3)
it went wrong like this:
但是当我输入:a =向量(1,2,3)就像这样:
TypeError: list() takes at most 1 argument (3 given)
TypeError: list()最多为1个参数(3个)
How can I dissolve it?
我该如何解散它?
2 个解决方案
#1
23
The problem is that you've misspelled the name of the constructor. Replace _init_
with __init__
.
问题是,您错误地拼写了构造函数的名称。用__init__替换_init_。
Here's the fixed code:
这是固定的代码:
class VECTOR(list) :
def __init__ (self,x=0.0,y=0.0,z=0.0,vec=[]) :
list.__init__(self,[float(x),float(y),float(z)])
if vec :
for i in [0,1,2] :
self[i] = vec[i]
a = VECTOR(1,2,3)
print(a)
And the demonstration that it works:
它的作用是:
% python test.py
[1.0, 2.0, 3.0]
I'd also like to give you a few additional comments:
我还想给你一些额外的评论:
- you should fix the coding style according to PEP8 (that's a document every Python developer should read entirely);
- 您应该根据PEP8修复编码风格(这是每个Python开发人员应该完全阅读的文档);
- you can probably do something more Pythonic (thanks Benjamin);
- 你可能会做一些更大的勾当(感谢本杰明);
- inheritance is not the only way to do that, you can also use an attribute to store the list and define the relevant methods (thanks Veedrac);
- 继承并不是唯一的方法,您还可以使用属性来存储列表并定义相关的方法(谢谢Veedrac);
- you could also use
super
(see paddyg's answer); - 你也可以使用super(见paddyg的答案);
edit note: I've added to this solution the relevant advises found in the comments.
编辑注:我已经在注释中添加了相关的建议。
#2
-2
EDIT if you call using super(VECTOR, list).__init__()
you don't need to pass self. Also you need to pass 1,2,3 as a list [1,2,3]
编辑如果你使用super(VECTOR, list).__init__()你不需要通过self。你也需要把1 2 3作为一个列表[1,2,3]
#1
23
The problem is that you've misspelled the name of the constructor. Replace _init_
with __init__
.
问题是,您错误地拼写了构造函数的名称。用__init__替换_init_。
Here's the fixed code:
这是固定的代码:
class VECTOR(list) :
def __init__ (self,x=0.0,y=0.0,z=0.0,vec=[]) :
list.__init__(self,[float(x),float(y),float(z)])
if vec :
for i in [0,1,2] :
self[i] = vec[i]
a = VECTOR(1,2,3)
print(a)
And the demonstration that it works:
它的作用是:
% python test.py
[1.0, 2.0, 3.0]
I'd also like to give you a few additional comments:
我还想给你一些额外的评论:
- you should fix the coding style according to PEP8 (that's a document every Python developer should read entirely);
- 您应该根据PEP8修复编码风格(这是每个Python开发人员应该完全阅读的文档);
- you can probably do something more Pythonic (thanks Benjamin);
- 你可能会做一些更大的勾当(感谢本杰明);
- inheritance is not the only way to do that, you can also use an attribute to store the list and define the relevant methods (thanks Veedrac);
- 继承并不是唯一的方法,您还可以使用属性来存储列表并定义相关的方法(谢谢Veedrac);
- you could also use
super
(see paddyg's answer); - 你也可以使用super(见paddyg的答案);
edit note: I've added to this solution the relevant advises found in the comments.
编辑注:我已经在注释中添加了相关的建议。
#2
-2
EDIT if you call using super(VECTOR, list).__init__()
you don't need to pass self. Also you need to pass 1,2,3 as a list [1,2,3]
编辑如果你使用super(VECTOR, list).__init__()你不需要通过self。你也需要把1 2 3作为一个列表[1,2,3]