What is the difference between __str__() and __repr__() in Python?Gable E
In Python, the__str__()
and__repr__()
methods are special methods used to define string representations of objects. Though they serve similar purposes, there are some differences between them. Here's a detailed explanation of the differences between__str__()
and__repr__()
:
__str__() method:
The__str__()
method is responsible for defining a user-friendly string representation of an object. It is intended to be used for displaying the object to end-users or for general human-readable output. The__str__()
method should return a string that provides a concise and readable description of the object.
1 2 3 4 5 6 7 8 9 10 11
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})" person = Person("Alice", 25) print(person) # Output: Person(name=Alice, age=25)
In this example, the__str__()
method is defined in thePerson
class to return a string representation of a person object. Whenprint(person)
is called, it invokes the__str__()
method, which returns a user-friendly string representation of the person object.
__repr__() method:
The__repr__()
method is responsible for defining a string representation that can be used to recreate the object. It is intended to provide a detailed and unambiguous representation of the object's internal state. The__repr__()
method should return a string that represents the object's state in a way that allows the object to be recreated using the string representation.
1 2 3 4 5 6 7 8 9 10 11
class Person: def __init__(self, name, age): self.name = name self.age = age def __repr__(self): return f"Person(name={self.name}, age={self.age})" person = Person("Alice", 25) print(person) # Output: Person(name=Alice, age=25)
In this example, the__repr__()
method is defined in thePerson
class to return a string representation that represents the internal state of a person object. Whenprint(person)
is called, it invokes the__repr__()
method, which returns a detailed string representation of the person object.
Difference between __str__() and __repr__():
The main differences between__str__()
and__repr__()
are:
1. Purpose:__str__()
is intended for user-friendly and readable output, while__repr__()
is intended for unambiguous and detailed representation for object recreation.
2. Usage:__str__()
is typically used byprint()
and thestr()
function, whereas__repr__()
is used by therepr()
function, as well as when the object is displayed in the interpreter.
3. Fallback behavior: If the__str__()
method is not defined, Python falls back to using the__repr__()
method as a default. This ensures that an object has a string representation, even if it is not explicitly defined.
4. Debugging and introspection: The__repr__()
method is commonly used during debugging and introspection to obtain a detailed representation of an object's internal state.
In summary, the__str__()
method provides a user-friendly string representation, while the__repr__()
method provides a detailed and unambiguous representation for object recreation and debugging. Depending on your specific needs, you can choose to define one or both methods in your classes to control how objects are represented as strings.
Similar Questions
What is the difference between __init__() and __new__() in Python?
What is the difference between __del__() and __exit__() in Python?
What is the difference between strip() and rstrip() in Python?
What is the difference between print() and return in Python?
What is the difference between read() and readline() in Python?
What is the difference between is and == in Python?
What is the difference between == and is in Python?
What is the difference between range() and xrange() in Python 2?
What is the difference between a list and a tuple in Python?
What is the difference between break and continue in Python?
What is the difference between assert and raise in Python?
What is the difference between append() and extend() methods in Python?
What is the difference between a property and an attribute in Python?
What is the difference between a class and an object in Python?
What is the difference between a module and a script in Python?
What is the difference between break and return in a loop in Python?
What is the difference between a decorator and a closure in Python?
What is the difference between a module and a package in Python?
What is the difference between a package and a module in Python?
What is the difference between a metaclass and a class in Python?