Learn Special Methods by Building a Vector Space - Step 23

Tell us what’s happening:

I am unable to understand where exactly he is saying me to put vars(self) can someone please help me?

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

# User Editable Region

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

# 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(v1.norm())
print(v2.norm())

Your browser information:

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

Challenge Information:

Learn Special Methods by Building a Vector Space - Step 23

" replacing the string returned by __str__() with a generator expression"

I think you might find this relevant

There’s also many other threads on this topic to review:
https://forum.freecodecamp.org/search?q=Learn%20Special%20Methods%20by%20Building%20a%20Vector%20Space%20-%20Step%2023%20order%3Alatest

so you want me to replace def __str__ with __str__() right?

Please remember to format your code

no, that’s not it, read again the sentence, it says “the string returned by”

If you look at the test output generated by your code:

// running tests
1. You should return a generator expression that iterates over vars(self).
2. You should return a generator expression that uses i as the iteration variable.
3. You should return a generator expression that calls getattr(self, i) for each item i in vars(self).
// tests completed

It’s saying that you should return a generator expression, not a string.

how can i run a generator expression? can someone help me i meant what does it look like.

you have a generator expression here inside other things, what issue are you having now writing it again?

so should i write it again?

You should not write exactly that, but you should write the generator expression.

The forum still is not here to tell you exactly what answer to write. That really doesn’t help you understand how to tackle similar problems in the future.

The hints are saying to return a generator expression. If you aren’t familiar with a “generator expression” do an Internet search and you can read about it.

You are returning a generator expression, but first you are converting it into a tuple and then you are converting it into a string, so in the end it returns a string. Don’t return a string, return a generator expression.

<>def __str__(self):
        return str(tuple(getattr(self, attr) for attr in vars(self)))<>

this is the code i have put but still its not taking can you guide me in correcting it?

I’ve edited your post to improve the readability of the code. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

Please attempt to correctly format your code.

You still have not made the changes discussed above. Please attempt to fix your code.

You are returning a string. Don’t return a string.

Please let me know if you have any questions

ok cool got it let me try and will update you on this

it worked guys thanks for helping me