Home >Backend Development >Python Tutorial >Define a string representation of an object using Python's __repr__() function

Define a string representation of an object using Python's __repr__() function

PHPz
PHPzOriginal
2023-08-22 12:05:111108browse

Define a string representation of an object using Pythons __repr__() function

Use Python's __repr__() function to define the string representation of an object

In Python programming, a common need is to customize the string representation of an object Representation form to facilitate program debugging and output. Python provides the __repr__() function, which can be used to define the string representation of an object.

__repr__() is a special method in Python that returns a printable string representation of an object. When we use the print() function to output an object, the __repr__() method of the object is actually called. The same mechanism works when we output an object directly to the console.

Below we use an example to demonstrate how to use the __repr__() function to define the string representation of an object.

class Student:
    def __init__(self, name, age, score):
        self.name = name
        self.age = age
        self.score = score

    def __repr__(self):
        return f"Student(name={self.name}, age={self.age}, score={self.score})"

In the above code, we define a Student class, which contains three attributes: name, age and score of the student, and use f-string in the __repr__() function to define the object String representation. In this way, when we use the print() function to output a Student object, we will get a string similar to "Student(name=Tom, age=18, score=90)".

Next, we try to instantiate a Student object and output its string representation.

student = Student("Tom", 18, 90)
print(student)

The output result is:

Student(name=Tom, age=18, score=90)

As can be seen from the results, we have successfully customized the string representation of the object, making the output more readable and convenient for debugging.

In addition to the __repr__() function, Python also provides the __str__() function to return the description string of the object. Unlike __repr__(), the __str__() function is more useful for providing human-readable strings, while __repr__() is more suitable for output during debugging and development.

We can see that using the __repr__() function to define the string representation of an object is very simple and flexible. By customizing the __repr__() function, we can flexibly control the output form of the object and provide more useful information.

To summarize, using Python's __repr__() function can easily customize the string representation of an object, improving code readability and debugging efficiency. Understanding and rationally using the __repr__() function is very helpful for writing excellent Python programs.

The above is the detailed content of Define a string representation of an object using Python's __repr__() function. 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