Learn Special Methods by Building a Vector Space - Step 52

Tell us what’s happening:

I’m totally lost on this one. Not sure what I’m supposed to do here.

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)

# User Editable Region

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

# 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 = 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}')

Your browser information:

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

Challenge Information:

Learn Special Methods by Building a Vector Space - Step 52

Let’s start with this:

You’ve used the type() function before, earlier in your code. This should be similar to the previous times you’ve used type. Can you see any differences in the way you’ve used it here?

Changed it:

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

and how it is going? do you have questions for this code?

Like every other function in Python, you cannot have a space between the name of the function and the brackets

print () #no
print() #yes

What are you trying to check here, if you put it into English words?

if type (other), (int, float):

An IF statement usually needs a comparison operator because you are comparing 2 things.

If this equals that
if A is greater than B then do this

Yeah, sorry that was just a typo, the space between type and (other).

Is this any better?

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

“If type(5) is equal to a tuple containing int and float”

It doesn’t make sense that 5 would be both int and float?

Do you test this out in a google collab doc or locally? You can test this out at the end of your code:

print(type(5))
if type(5) == (int, float):
    print("both")

if type(5) == int:
    print("int")

if type(5) == float:
    print("float")

Test it, don’t just make wild guesses and then give up when it doesn’t work

Sorry but your last post was over my head. I don’t know what you’re talking about and have never used google collab. Could you break down what your were trying to say here?

Put this at the end of your code

Google collab is a website/coding platform and it’s easy to open a window and test out some Python code. I use it a lot if I want to test something quickly and not mess up something I’m working on in fCC.

You need to test your ideas out to see if they work and play around with it and print results to understand what you’re doing. Google Colab is a good platform for that.

if type(5) == (int, float):

This is never going to work because type(5) is not equal to (int, float)

You are on the right track though, getting there.

Thanks I did try to put that after my code, but the first time I didn’t realize that copy / pasting messed with the indentation. The way it was it didn’t print anything or throw any errors.

I see what it printed, but I don’t know how to handle that. Do I need two separate statements, one for each option?

Sounds like it might work try it out.

There are a few ways this could be implemented, as long as it works

Like I said I’m completely lost on this step. Here’s what I’ve got so far:

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

your return is only inside the second if, that means that if the first run, the return doesn’t

That’s right, updated it:

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

1 Like

Ok did you have any other questions? This passes the test.