Learn Special Methods by Building a Vector Space - Step 17

Tell us what’s happening:

What am I doing wrong? If I add ‘*’ as second argument after self I will get an error and if I use the method I use here there will not error but I am passing the test.

Your code so far


# User Editable Region

class R2Vector:
    def __init__(self, **kwargs):
        self.x = kwargs.get('x')
        self.y = kwargs.get('y')
        
    def norm(self):
        return (self.x**2 + self.y**2)**0.5
        
    def __str__(self):
        return f'{self.x, self.y}'

class R3Vector(R2Vector):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.z = kwargs.get('z')

v1 = R2Vector(x=2, y=2)
print(v1.norm())
print(v1)
v2 = R3Vector(x=2, y=2, z=3)

# User Editable Region

Your browser information:

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

Challenge Information:

Learn Special Methods by Building a Vector Space - Step 17

Please, read again the instructions. You are going in a different direction from what has been asked. If you have def __init__(self, x, y): and you need to add a * as the second parameter, you don’t want to change the other parameters.

To enforce keyword arguments you should have something like def foo(a, b, *, c, d ):. Then when you call the function, c and d must be keyword arguments.

Hi @pythoncomedian

Here is a comparison of the original code and your code.

The code in blue is the original code, the code in red is your code.
The code in magenta is the overlap.

  1. In Python, you can enforce the use of keyword-only arguments by adding a * as an additional argument to the function or method signature.
    Modify both __init__ methods by adding a * as the second parameter (after self). Every parameter placed after that will require the use of a keyword argument in the function/method call. ✗

For part 1 add the * as a second argument. Do not alter the rest of the expression.

  1. This means that you need to modify the super().__init__(x, y) call, too. Do it by giving x the value x, and y the value y. ✗

  2. Finally, modify the instantiation of v1 and v2 by using keyword arguments. ✓

You correctly modified the instantiation of v1 and v2.

Happy coding

2 Likes

Modifying the super method by giving x the value of x and also for y. This is what I am doing:
self.x = x
self.y = y
and it is not working

These are the two lines you need to modify for part 1.

image
image

I have modified those already. The issue is from the super method.

But how did you modify them??

1 Like

So the forum can assist please post your full code.

Use the following method to post code to the forum:

  1. On a separate line type three back ticks.
  2. On a separate line paste your code.
  3. On the last line type three back ticks. Here is a single back tick `

Happy coding

Here is it:

class R2Vector:
    def __init__(self,*, x, y):
        self.x = x
        self.y = y
        
    def norm(self):
        return (self.x**2 + self.y**2)**0.5
        
    def __str__(self):
        return f'{self.x, self.y}'

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

v1 = R2Vector(x=2, y=3)
print(v1.norm())
print(v1)
v2 = R3Vector(x=2, y=2, z=3)

Now you need to modify the super call, since you enforced keyword args:

Do it by giving x the value x, and y the value y.

Is that not what I am doing?:

super().__init__(x,y)
        self.x = x
        self.y = y
        self.z = z
     

You should change only this line:

You need to modify the arguments of __init__, because they must be keyword arguments after you enforced them.

Thanks.
I have done that.

super().__init__(x=x, y=y)
1 Like