Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

list() takes at most 1 argument (3 given)

Tags:

python

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?

like image 572
Reginold Lu Avatar asked May 06 '15 07:05

Reginold Lu


1 Answers

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:

  • you should fix the coding style according to PEP8 (that's a document every Python developer should read entirely);
  • 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);
  • you could also use super (see paddyg's answer);

edit note: I've added to this solution the relevant advises found in the comments.

like image 89
Maxime Chéramy Avatar answered Sep 23 '22 00:09

Maxime Chéramy