Python's Magic Methods in Classes (Quiz)

Become a Member to take the quiz.

Avatar image for bhchurch6

bhchurch6 on Nov. 6, 2024

I added the methods requested with the following code. As indicated in a previous question, single quotes can be used to deliminate an 'f' string. The answer provided does not allow again for this option to be used.

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def __str__(self):
        # Implement user-friendly string representation
        return f'Person: {self.name}, Age: {self.age}'  

    def __repr__(self):
        # Implement developer-friendly string representation
        return f'{self.__class__.__qualname__}(name="{self.name}", age={self.age})'

This code does produce the required result for a Person('Sonika', 30) object.

Become a Member to join the conversation.