Learn Special Methods by Building a Vector Space - Step 23

Tell us what’s happening:

def str(self):
return “({})”.format(", ".join(str(getattr(self, i)) for i in vars(self)))
the error that I have : you should return a generator expression that iterates over vars(self)

Your code so far


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

# User Editable Region

    def __str__(self):
        return "({})".format(", ".join(str(getattr(self, i)) for i in vars(self)))


# User 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())
print(v1)

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:134.0) Gecko/20100101 Firefox/134.0

Challenge Information:

Learn Special Methods by Building a Vector Space - Step 23

You should just return a generator expression, not a string of any kind

def str(self):
return “(” + “, “.join(f”{getattr(self, key)}” for key in vars(self)) + “)”
I try this solution but I have this error: You should return a generator expression that iterates over

vars(self)

how can I fix it?

do not return a string, return only the generator expression

If what you are returning is in “quotes” then it’s a string. Don’t return a string, return a generator expression.