Learn Special Methods by Building a Vector Space - Step 22

Tell us what’s happening:

Hi. I keep getting an error message:
You should modify your norm method to use vars(self) instead of self.dict.

I am using vars(self) in my norm calculation and my v1 and v2 norms are correct and printing out. Are we supposed to use vars(self) in a different way?

3.605551275463989
4.123105625617661

Your code so far


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

# User Editable Region

    def norm(self):
        return sum(vars(self)[str(val)]**2 for val in vars(self))**0.5

# User Editable Region

    def __str__(self):
        return f'{self.x, self.y}'

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/116.0.0.0 Safari/537.36

Challenge Information:

Learn Special Methods by Building a Vector Space - Step 22

You’ve used vars() correctly but you only need to replace self.__dict__, no other changes

Instead of directly accessing the __dict__ attribute of self… use the vars() function.

1 Like

Oh I see, I overdid it :sweat_smile:
Thanks!

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.