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