Home  >  Article  >  Backend Development  >  How to use the __str__() function in Python to define the string representation of an object

How to use the __str__() function in Python to define the string representation of an object

WBOY
WBOYOriginal
2023-08-21 22:55:541292browse

How to use the __str__() function in Python to define the string representation of an object

How to use the __str__() function in Python to define the string representation of an object

In Python, each object has a string representation, using Displays the contents of an object when printing it. Often, the default string representation of an object does not meet our needs. In order to customize the string representation of an object, we can use the __str__() function in Python.

__str__() function is a special method used to convert objects into strings. When we print an object, Python automatically calls the object's __str__() function and prints its return value as a string.

Here is a simple example that demonstrates how to use the __str__() function in Python to define a string representation of an object:

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("Tom", 25)
print(person)

In the above code, we define a A class named Person, which has two properties: name and age. We define a __str__() function in the class and return a formatted string that contains the object's attribute values.

When we create a Person object and print it, Python will automatically call the __str__() function of the object and print its return value. The output result will be:

Person(name=Tom, age=25)

By defining the __str__() function, we can display the attribute values ​​of the object in a more intuitive way, making it easier for us to debug and understand the program. We can customize the string format in the __str__() function according to our own needs and precisely control the printed content.

In addition to the __str__() function, Python also provides another special method __repr__(). The string returned by the __repr__() method should be a valid Python expression that can be used to create the object. If we do not define the __str__() function, Python will call the object's __repr__() function by default to print the object.

Here is an example that demonstrates how to use the __repr__() method:

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("Tom", 25)
print(person)

This code is almost the same as the previous example code, the only difference is that we use __repr__() method instead of the __str__() method. The output is still the same:

Person(name=Tom, age=25)

It should be noted that the __str__() method is mainly used to provide a descriptive string for the object, while the __repr__() method is mainly used to provide a string for reconstructing the object. string. In actual development, we can choose which method to use according to needs, or define two methods at the same time to provide a more comprehensive string representation.

The above is the detailed content of How to use the __str__() function in Python to define the string representation of an object. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn