Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
repr and str | Magic Methods
In-Depth Python OOP
course content

Course Content

In-Depth Python OOP

In-Depth Python OOP

1. OOP Concepts
2. Inheritance
3. Encapsulation
4. Polymorphism and Abstraction
5. Magic Methods

bookrepr 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:

1234567
class User: def __init__(self, username): self.username = username instance = User("top.user.123") print(instance)
copy

To obtain a more understandable and informative output when printing an instance, you can define the __repr__ magic method, which stands for "representation".

12345678910
class User: def __init__(self, username): self.username = username def __repr__(self): return f"User: {self.username}" instance = User("top.user.123") print(instance)
copy

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.

12345678910111213
class 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)
copy

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.

1234567891011121314
class 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)
copy

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.

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 5. Chapter 2
some-alt