Learn Special Methods by Building a Vector Space - Step 23

Tell us what’s happening:

I don’t understand what I am doing wrong here, I think I have the correct attributes and objects within the getattr() function. It’s a generator, and it iterates over vars(self). What’s left???

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) (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 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36

Challenge Information:

Learn Special Methods by Building a Vector Space - Step 23

this is your generator, the other part is not in it

Ahhhh, sorry. I was reading one of the forum posts on this step and someone mentioned generators in brackets. In my head I thought that brackets are these [ ] not these ().

I understand now that both are called brackets.

Regardless, the code still doesn’t pass when I remove the [ ].

what is your code now? removing the square brackets isn’t the only thing you need to do

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

the getattr needs to be inside the generator expression, not the opposite

So like this? Still doesn’t work.

Am I misundertanding how generator expressions work?

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

the generator expression is made of three parts: (expression for var in something)
you are nesting things in a way that I don’t understand, and right now your generator is still only this (i for i in vars(self))

I got it, I just had to rearrange the brackets. Thanks for your help