Learn Special Methods by Building a Vector Space - Step 52

Tell us what’s happening:

I have written following code:
def mul(self, other):
if isinstance(other, int) or isinstance(other, float):
kwargs = {i: ( other * getattr(self, i) ) for i in vars(self)}
return self.class(**kwargs)

but it doesn’t pass.
Hint tells me that - You should return a new instance of the current class only when the type of other is either int of float.

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 isinstance(other, int) or isinstance(other, float):
            kwargs = {i: ( other * getattr(self, i) ) 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}')
v5 = v1 * 3
print(v5)
v6 = R3Vector( x=12, y=23, z=34)
print(v6)

v7 = v6 * ''
print(v7)

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 52

Try to use type() instead of isinstance().

3 Likes

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

It worked thanks

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

I don’t understand the answer -
type(other) resolves to R2Vector - will never be int or float.,never resolve to true.
The return must be return self.class(**kwargs).
Don’t know verify that the vars, x and y in this case are both float or int.
besides it is not an acceptable answer.
response: “Sorry, your code does not pass. You’re getting there.”
Not good at python and confused as s*t

‘other’ here is not another object, instead, it might be an int or float (hence the conditional above). So, u don’t need to apply getattr to ‘other’:

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

start with the big issue. The following code does not pass the code check.

This is what I’m not understanding about this exercise. The issue of ‘other’ being a class or numeric type.

If I’m writing an overload to multiply an int with a class then the code works.
v1 = R2Vector(x=2, y=3)
v3= v1 * 2

If the multiplier overload is to multiply two instances of the same class. As we create the __add or __sub methods . This will never work.
Since ‘other’ is a class, not float or int.
v1 = R2Vector(x=2, y=3)
v2 = R2Vector(x=5, y=6)
v3= v1 * v2

So I’m stuck, not being able to pass the step, and confused.

In the future, please create your own topic when you have specific questions about your own challenge code. Only respond to another thread when you want to provide help to the original poster of the other thread or have follow up questions concerning other replies given to the original poster.

The easiest way to create a topic for help with your own solution is to click the Ask for Help button located on each challenge. This will automatically import your code in a readable format and pull in the challenge url while still allowing you to ask any question about the challenge or your code.

Thank you.