Back to Freecodecamp

Step 14

curriculum/challenges/english/blocks/learn-special-methods-by-building-a-vector-space/65f407ea37ad6e181b90462e.md

latest1.1 KB
Original Source

--description--

You could assign each i parameter to self.i as you did before. Although in this case you have few lines to repeat, one way to avoid this repetition is using the super() function. super() enables you to refer implicitly to the parent class: super().__init__(x, y) calls the __init__ method of the parent class.

Add a super().__init__(x, y) call to your __init__ method.

--hints--

You should have a super().__init__(x, y) call in your __init__ method.

js
({
    test: () => assert(runPython(`
      _Node(_code).find_class("R3Vector").find_function("__init__").has_stmt("super().__init__(x, y)")
    `))
})

--seed--

--seed-contents--

py
class R2Vector:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        
    def norm(self):
        return (self.x**2 + self.y**2)**0.5
        
    def __str__(self):
        return f'{self.x, self.y}'
--fcc-editable-region--
class R3Vector(R2Vector):
    def __init__(self, x, y, z):
        pass
--fcc-editable-region--
v1 = R2Vector(2, 3)
print(v1.norm())
print(v1)