Learn Special Methods by Building a Vector Space - Step 23

Tell us what’s happening:

whats wrong with my str() method generator expression. I dont get it :confused:

##error:
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 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())

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36

Challenge Information:

Learn Special Methods by Building a Vector Space - Step 23

There’s a problem with the str method in your R2Vector class. The way you’re using getattr isn’t quite right.

def str(self):
return getattr(self,(i for i in vars(self)))

Recorda como funciona getattr(objeto, cadena(nombre del atributo))

Entonces lo que tenes que hacer a la vez que iteras asignarle ese valor i al metodo, es decir:

def str(self):
return (getattr(self, i) for i in vars(self)))

Se entiende?

1 Like