The problem is that you mistakenly wrote 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 a demonstration that it works:
% python test.py [1.0, 2.0, 3.0]
I would also like to give you some additional comments:
- You must correct the coding style in accordance with PEP8 (for each Python developer to read the document completely);
- you can probably do something more pythonic (thanks to Benjamin);
- Inheritance is not the only way to do this, you can also use the attribute to store the list and determine the appropriate methods (thanks Veedrac);
- you can also use
super (see paddyg answer);
Editing Note: I have added relevant recommendations in the comments to this decision.
Maxime chΓ©ramy
source share