🙌

Python __str__ vs __repr__

2023/05/31に公開

Have you ever thought about how you can make your custom Python objects more readable and easy to understand? Well, that’s where the Python str and repr methods come into the picture. It’s like a magical method that gives us the ability to decide how our objects should appear when we convert them into strings. In this blog, we’ll dive into the fascinating world of the str and repr methods and explore how they can elevate your programming skills.

What is Python str function?

The str method in Python is a special function that lets us define how objects should be represented as strings. It provides a way for objects to introduce themselves in a human-readable format. When we print an object or use the str() function, Python looks for the str method and calls it to get the string representation.

If the str method is not explicitly defined for a class, the default implementation provided by the interpreter will be used. This default implementation returns a string containing the class name of the object and its memory address.

Syntax of str in Python

When it comes to understanding the syntax of the str method in Python, we’ll break it down into simple steps. By following these steps, you’ll be able to implement the str method effortlessly in your classes.

  1. Class Definition: To begin, we need to define our class. We use the class keyword followed by the name of our class. Inside the class, we define the str method, which will determine how our object is represented as a string.
  2. Method Definition: Within the class, we start defining the str method. It’s important to note that the method name begins and ends with two underscores (underscores are like little huggers for the name).
  3. Return the String Representation: Inside the str method, we need to specify what the method should return. This return value will be the string representation of our object. You can include any information you desire within this string representation, such as attribute values or custom messages.
  4. Complete the Syntax: Lastly, we need to make sure that we close our method definition and class definition properly. This is done by adding appropriate indentation and ensuring that everything lines up correctly.
    Here’s an example to illustrate the syntax of str in action:
class Dog:
    def __init__(self, name, breed):
        self.name = name
        self.breed = breed

    def __str__(self):
        return f"Meet {self.name}, the adorable {self.breed}."

my_dog = Dog("Max", "Labrador Retriever")
print(my_dog) # Meet Max, the adorable Labrador Retriever.

In this example, we define a class called Dog with attributes name and breed. The str method is implemented to provide a string representation of a Dog object. When we print my_dog, the str method is automatically called, and it returns a string with the dog’s name and breed.

If the str method is not defined in the class and we attempt to print the object, the output would display the memory location of the class instead.

<__main__.Dog object at 0x7f5887f2d210>

Learn more about Python str and repr method from the original post.
Find more blog about Python.

Discussion