Learn Special Methods by Building a Vector Space - Step 74

Tell us what’s happening:

from other people code on the forum i see people get the right answer but i don’t know how, i have some questions, for this dictionary how do i know where the keys go? Also another question what do i return and why, and why do i do a list comprehension

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

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

    def __repr__(self):
        arg_list = [f'{key}={val}' for key, val in vars(self).items()]
        args = ', '.join(arg_list)
        return f'{self.__class__.__name__}({args})'

    def __add__(self, other):
        if type(self) != type(other):
            return NotImplemented
        kwargs = {i: getattr(self, i) + getattr(other, i) for i in vars(self)}
        return self.__class__(**kwargs)

    def __sub__(self, other):
        if type(self) != type(other):
            return NotImplemented
        kwargs = {i: getattr(self, i) - getattr(other, i) for i in vars(self)}
        return self.__class__(**kwargs)

    def __mul__(self, other):
        if type(other) in (int, float):
            kwargs = {i: getattr(self, i) * other for i in vars(self)}
            return self.__class__(**kwargs)        
        elif type(self) == type(other):
            args = [getattr(self, i) * getattr(other, i) for i in vars(self)]
            return sum(args)            
        return NotImplemented

    def __eq__(self, other):
        if type(self) != type(other):
            return NotImplemented
        return all(getattr(self, i) == getattr(other, i) for i in vars(self))
        
    def __ne__(self, other):
        return not self == other

    def __lt__(self, other):
        if type(self) != type(other):
            return NotImplemented
        return self.norm() < other.norm()

    def __gt__(self, other):
        if type(self) != type(other):
            return NotImplemented
        return self.norm() > other.norm()

    def __le__(self, other):
        return not self > other

    def __ge__(self, other):
        return not self < other

# User Editable Region

class R3Vector(R2Vector):
    def __init__(self, *, x, y, z):
        super().__init__(x=x, y=y)
        self.z = z
        
    def cross(self, other):
        if type(self) != type(other):
            return NotImplemented
        kwargs = { getattr(self,y) * getattr(self,z) - getattr(self,z) * getattr(self,y),
         getattr(self,z) * getattr(self,x) - getattr(self,x) * getattr(self,z),
        getattr(self,x) * getattr(self,y) - getattr(self,y) * getattr(self,x)}
        return R3Vector(R2Vector)

# User Editable Region

v1 = R2Vector(x=2, y=3)
v2 = R2Vector(x=0.5, y=1.25)
print(f'v1 = {v1}')
print(f'v2 = {v2}')
v3 = v1 + v2
print(f'v1 + v2 = {v3}')
v4 = v1 - v2
print(f'v1 - v2 = {v4}')
v5 = v1 * v2
print(f'v1 * v2 = {v5}')

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) 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 74

kwargs should be a dictionary, but you are not setting the keys.
In this case, your are not forced to use getattr because you are not iterating over the atributes.
Finally, what are you trying to achieve here?

I haven’t done this project yet (or much of the new beta curriculum yet) so it’s hard for me to understand if this is building from previous lessons but, the instructions here seem a bit sparse?

Most of the previous steps have been using getattr() so it makes sense that’s why people are using it here. Maybe we can add a note “Do not need to use getattr() here because of the formula. Access the object properties directly” ? Because syntactically you can use getattr() here, right?

Instructions never mention a dictionary, but I’m not sure if that should be obvious from context / previous steps? EDIT: ok I can see some other operations are built and returned in the same format

return the resulting vector

You will return a new vector in a similar fashion to what you returned for other operation methods (add, subtract, multiply). Remember this?

return a new vector object. Use __class__ and unpack kwargs to instantiate the new vector.

Review how to create a dictionary:
https://www.w3schools.com/python/python_dictionaries.asp

https://www.geeksforgeeks.org/how-to-create-a-dictionary-in-python/

The keys for this dictionary will be the variables of the object.

kwargs = {i: getattr(self, i) - getattr(other, i) for i in vars(self)}

Do you fully understand what you did with this line, 100%? Your dictionary here will be the same, but

  • you will not use a comprehension
  • you won’t use getattr(), access the object properties directly.
  • use the formula (you’ve implemented that correctly already)

The implementation is free. The only thing that is checked is the result here.

You can use getattr if you want to, and I see that coming from the previous steps it may seem reasonable. Because of how the cross product is computed, it would be cleaner without getattr, but the result is the same.

“x” : getattr(self,y) * getattr(self,z) - getattr(self,z for ) * getattr(self,y),
“y” : getattr(self,z) * getattr(self,x) - getattr(self,x) * getattr(self,z),
“z” :getattr(self,x) * getattr(self,y) - getattr(self,y) * getattr(self,x)
} i’ve now got this, and to answer your final question the instructions told me to do that but what should i return? the instructions from what i’ve realised are very very vague i sometimes even ask chatgpt to do certain things and they try and follow the instructions and fail.

“x” : getattr(self,y) * getattr(self,z) - getattr(self,z for ) * getattr(self,y),
“y” : getattr(self,z) * getattr(self,x) - getattr(self,x) * getattr(self,z),
“z” :getattr(self,x) * getattr(self,y) - getattr(self,y) * getattr(self,x)
} also no i don’t understand, could you not do it without the getattr, and if so how?

How do you access variables or methods stored in an object? How did you do it before using getattr()? Look how you set the variables earlier in the code. Do an internet search. Look at the Sudoku solver project or the budget app project.

You need to try to rely on your own abilities to find answers.

1 Like