Which method would you override to change the string representation of a custom object? __str____repr____string____reprstr__
Question
Which method would you override to change the string representation of a custom object? str____repr____string____reprstr
Solution
To change the string representation of a custom object in Python, you would override the __str__ or __repr__ method.
Here's how you can do it:
- Define your class. For example, let's create a class named
Person.
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
- Override the
__str__method. This method should return a string, and it will be called when you use thestr()function on an instance of your class, or when you try to print the instance.
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def __str__(self):
return f'Person(name={self.name}, age={self.age})'
- Similarly, you can override the
__repr__method. This method is used to return a string that represents a printable version of the object. It's called by built-in functionrepr(), and by backticks in earlier versions of Python.
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def __str__(self):
return f'Person(name={self.name}, age={self.age})'
def __repr__(self):
return f'Person(name={self.name}, age={self.age})'
Note: If you only implement one of these methods, Python will use it as a fallback for the other if needed. If possible, __repr__ should return a string that reproduces the exact object when fed to eval().
Similar Questions
In Python, the __str__ method is used to ___.Choice 1 of 6:Change the object itself into a stringChoice 2 of 6:Provide a human-readable string representation of an objectChoice 3 of 6:Change how str() worksChoice 4 of 6:Modify all values store in the class into an equivalent stringChoice 5 of 6:Overwrite the object its call on with a stringChoice 6 of 6:Redefine the str type
Why is overriding the method for string representation of objects useful?It is impossible to forget important attributes during output.It maximizes the programming effort.Many complicated formatting instructions lead to clear code.The textual representation of a class is then centrally defined.
In Python, what is the purpose of the __str__ method in a class?AConvert the object to a string representationBDefine a string variableCConcatenate strings in the classDPrint the class definition
What is __repr__?Instance method that returns an “official” string representation of an instanceInstance method that prints an “official” string representation of an instanceInstance method that returns the dictionary representation of an instance
What is __repr__?Instance method that prints an “official” string representation of an instanceInstance method that returns an “official” string representation of an instanceInstance method that returns the dictionary representation of an instance
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.