Learn Special Methods by Building a Vector Space - Step 23

Help with Issue #23__str__ Method for Object Attributes

Hi everyone,

I’m facing an issue with customizing the __str__ method in Python to display all the attributes of an object. I’ve tried several approaches, but none of them seem to work as expected. Here are the versions I’ve attempted:

def __str__(self):
    return f'{getattr(self, i) for i in vars(self)}'

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

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

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

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

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

I then try to print the object like this:

print(v2)

However, none of these implementations produce the desired result.

Does anyone know how to fix this or has encountered a similar problem? Any advice or working examples would be greatly appreciated!

Thanks in advance!

Yeah, with what is required by this step print(v2) will result in error. That will be further changed in following steps.


For future - please link to the challenge in question, or use the Help button on the challenge page. It will make helping easier :slightly_smiling_face:

1 Like