repr and str
Let's explore some of the most useful magic methods.
We're already familiar with the __init__
magic method, which we considered earlier. However, there are many other magic methods that can be incredibly useful. In this section, we'll focus on two of them: __repr__
and __str__
.
Class Representation
When you create an instance of your class and try to print it in the console, you may encounter a representation that may not be very informative or user-friendly:
To obtain a more understandable and informative output when printing an instance, you can define the __repr__
magic method, which stands for "representation".
The provided code defines a class called User
. The User
class includes a magic method __repr__
, which returns a string representation of the object. In this case, the __repr__
method returns a formatted string that includes the class name "User" and the value of the username
attribute.
When the print
statement is executed, it calls the __repr__
method of the instance
object to obtain a string representation of the object. In this case, the __repr__
method is overridden in the User
class to provide a custom representation.
String Representation
The __str__
magic method functions similarly to __repr__
, but it is specifically used to provide information and a more readable representation of an object to users.
The print()
function searches for the __str__
magic method in an object to obtain a string representation. If the __str__
method is not defined, it falls back to using the __repr__
method.
Additionally, the __str__
magic method can be used to convert an instance to a string type explicitly. By implementing the __str__
method, you can define how the object should be represented as a string.
Note
In summary, the
__str__
and__repr__
magic methods are used to define different string representations of objects in Python. The__str__
method is typically used to provide a user-friendly output, presenting the object in a way that is easy to understand for users. On the other hand, the__repr__
method is used to provide a developer-friendly output, typically containing more detailed information about the object for debugging and internal use.
Tudo estava claro?
Conteúdo do Curso
In-Depth Python OOP
In-Depth Python OOP
4. Polymorphism and Abstraction
repr and str
Let's explore some of the most useful magic methods.
We're already familiar with the __init__
magic method, which we considered earlier. However, there are many other magic methods that can be incredibly useful. In this section, we'll focus on two of them: __repr__
and __str__
.
Class Representation
When you create an instance of your class and try to print it in the console, you may encounter a representation that may not be very informative or user-friendly:
To obtain a more understandable and informative output when printing an instance, you can define the __repr__
magic method, which stands for "representation".
The provided code defines a class called User
. The User
class includes a magic method __repr__
, which returns a string representation of the object. In this case, the __repr__
method returns a formatted string that includes the class name "User" and the value of the username
attribute.
When the print
statement is executed, it calls the __repr__
method of the instance
object to obtain a string representation of the object. In this case, the __repr__
method is overridden in the User
class to provide a custom representation.
String Representation
The __str__
magic method functions similarly to __repr__
, but it is specifically used to provide information and a more readable representation of an object to users.
The print()
function searches for the __str__
magic method in an object to obtain a string representation. If the __str__
method is not defined, it falls back to using the __repr__
method.
Additionally, the __str__
magic method can be used to convert an instance to a string type explicitly. By implementing the __str__
method, you can define how the object should be represented as a string.
Note
In summary, the
__str__
and__repr__
magic methods are used to define different string representations of objects in Python. The__str__
method is typically used to provide a user-friendly output, presenting the object in a way that is easy to understand for users. On the other hand, the__repr__
method is used to provide a developer-friendly output, typically containing more detailed information about the object for debugging and internal use.
Tudo estava claro?