I want to get the vector like: 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:
TypeError: list() takes at most 1 argument (3 given)
How can I dissolve it?
The problem is that you've misspelled the name of the constructor. Replace _init_
with __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:
super
(see paddyg's answer);edit note: I've added to this solution the relevant advises found in the comments.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With