Back to Freecodecamp

Step 24

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

latest1000 B
Original Source

--description--

To obtain a proper string representation, call the tuple() constructor and pass it the generator expression you wrote in the previous step as the argument.

--hints--

You should pass the expression returned by the __str__ method to the tuple() constructor.

js
({ test: () => assert(runPython(`_Node(_code).find_class("R2Vector").find_function("__str__").has_return("tuple(getattr(self, i) for i in vars(self))")`)) })

--seed--

--seed-contents--

py

class R2Vector:
    def __init__(self, *, x, y):
        self.x = x
        self.y = y

    def norm(self):
        return sum(val**2 for val in vars(self).values())**0.5
--fcc-editable-region--
    def __str__(self):
        return (getattr(self, i) for i in vars(self))
--fcc-editable-region--
class R3Vector(R2Vector):
    def __init__(self, *, x, y, z):
        super().__init__(x=x, y=y)
        self.z = z

v1 = R2Vector(x=2, y=3)
v2 = R3Vector(x=2, y=2, z=3)
print(v1.norm())
print(v2.norm())