Learn Special Methods by Building a Vector Space - Step 30

Tell us what’s happening:

I do not understand the test. This is up to the level I understood.

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)))

# User Editable Region

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

# 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 = R3Vector(x=2, y=2, z=3)
print(f'v1 = {v1}')
print(f'v2 = {v2}')

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 30

1 Like

This alone would raise an error. You need to reference the object of which you want to get the name. How do you reference the current instance in Python?

Then, think that the string used to instantiate the object should have this structure:
ClassName(arg1, arg2, ..., argN)

Once you add the reference to the current instance so that you are able to get the class name, you’d still have this instead:
ClassName arg1, arg2, ..., argN

1 Like

By referencing the object that will be self.__class__.__name__(). If the parenthesis will there when I passed in the args argument it did not work. If the parenthesis will not be there then where do I pass the args argument?

2 Likes

You are writing a string, you are allowed to write things outside the replacement fields ({}).

1 Like

If I write the args variable out the replacement field then it is now an ordinary string which means the args variable was not used. Without it the test is still not working

1 Like

I’m saying you should write parentheses outside the replacement fields.

1 Like

Aha!!! Thanks, it worked. Why does this f'{self.__class__.__name__}({args})' works and this f'{self.__class__.__name__(args)}' don’t?

2 Likes

It doesn’t work because self.__class__.__name__ returns a string and so it’s not callable. If you append a pair of parentheses to it, it would raise an error when evaluated.

3 Likes

Thanks. I will try to rap my head around it.

2 Likes

Thanks for this conversation which has also helped me!

2 Likes

thanks for this conversation it has help me alot.

1 Like

i have args = ', '.join(arg_list)
You should use class.name to build the f-string needed to instantiate a vector object and return it from the repr method.

def repr(self):
arg_list = [f’{key}={val}’ for key, val in vars(self).items()]
args = ‘, ‘.join(arg_list)
return f’{class.name} {args}’
print(f’v1 = {v1.repr()}') = v1 = R2Vector x=2, y=3

but this solution does not for me.

Sorry - correctly prin the ocde.
You should use class.name to build the f-string needed to instantiate a vector object and return it from the repr method.

def __repr__(self):
        arg_list = [f'{key}={val}' for key, val in vars(self).items()]
        args = ', '.join(arg_list)
        return f'{__class__.__name__} {args}'
print(f'v1 = {v1.__repr__()}') = v1 = R2Vector x=2, y=3

nor does this works

   return f'{__class__.__name__}({args})'

which returns v1 = R2Vector(x=2, y=3)

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

Sorry, your code does not pass. Keep trying.

You should use __class__.__name__ to build the f-string needed to instantiate a vector object and return it from the __repr__ method.

You forgot the “self” at the beginning. It should be:

self.__class__.__name__

thanks - that was it. :slight_smile:

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.