Back to Freecodecamp

Step 12

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

latest1.3 KB
Original Source

--description--

Inheritance enables you to define a class from an existing one. The new class, called child, inherits all the methods and properties of the existing class, called parent.

py
class Tree:
    def sprout(self):
        print('Making new leaves!')

class Oak(Tree):
    pass
    
Oak().sprout() # Output: Making new leaves!

In the example above, the child class Oak inherits from Tree and inherits the sprout method from the parent class Tree.

Create a new class named R3Vector and follow the example above to make it inherit from the R2Vector class.

--hints--

You should define a new class R3Vector.

js
({
    test: () => assert(runPython(`
      _Node(_code).has_class("R3Vector")
    `))
})

Your new class R3Vector should inherit from R2Vector.

js
({
    test: () => assert(runPython(`
      _Node(_code).find_class("R3Vector").inherits_from("R2Vector")
    `))
})

--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--

--fcc-editable-region--
v1 = R2Vector(2, 3)
print(v1.norm())
print(v1)