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:
1234567class User: def __init__(self, username): self.username = username instance = User("top.user.123") print(instance)
To obtain a more understandable and informative output when printing an instance, you can define the __repr__
magic method, which stands for "representation".
12345678910class User: def __init__(self, username): self.username = username def __repr__(self): return f"User: {self.username}" instance = User("top.user.123") print(instance)
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.
12345678910111213class User: def __init__(self, username): self.username = username def __repr__(self): return f"User: {self.username}" def __str__(self): return f"This is a user {self.username}" instance = User("top.user.123") print(instance)
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.
1234567891011121314class User: def __init__(self, username): self.username = username def __repr__(self): return f"User: {self.username}" def __str__(self): return f"{self.username}" instance = User("top.user.123") string = str(instance) + " is the best user!" print(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.
Takk for tilbakemeldingene dine!
Spør AI
Spør AI
Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår