python-object-oriented-20-override __str__ method / __ repr__ method

Posted May 27, 20202 min read

1. Basic introduction

The object class has a __str __() method, which is used to return an "object description", corresponding to the built-in function str(), and is often used in the print() method to help us view the object information.
Because __str __() is defined in the object class by default, subclasses can override this method.

The corresponding method names in different languages:

  1. Python language:__str __() method
  2. JAVA language:toString() method
  3. PHP language:__toString() method

2. Method definition

__str __() is defined by default in class object.

def __str __(self, * args, ** kwargs):# real signature unknown
        "" "Return str(self)." ""

3. Simple example

Sample code:

class Student(object):

class Person(object):
    def __init __(self, name, age): = name
        self.age = age

    def __str __(self):
        return "Name:{0}, age:{1}". format(, self.age)

s1 = Student()
print("-" * 20)

p1 = Person("Nie Fajun", 100)

operation result:

<__ main __. Student object at 0x0000015E99B5F3C8>
Name:Nie Fajun, Age:100

Procedure description:

  1. The class Student directly inherits the class object, so __str __() is also directly used, so the format of the printed content is as follows:<module name. Class name The format of the inherited class name at memory address>
  2. Class Person inherits the class object, but defines __str __(), which belongs to the subclass and overrides the parent class method, using the subclass s __str __() method, so when printing is executed, the output:Name:Nie Fajun Age:100.

4. The difference between __str__ and __repr__

The str() function and __str__ are seen a lot in daily development, but few people may notice __repr__. These two are very close, but there are some subtle differences.
Similarities:Both are functions of the printout function
The difference:__str__ is displayed for the user, and it is common in both IDE and command line. __repr__ is for program display, only appears on the command line and is rare.
Sample code:

>>> class Person:
... def __init __(self, name, age):
... = name
... self.age = age
... def __str __(self):
... return "__str __:{0}, {1}". format(, self.age)
... def __repr __(self):
... return "__repr __:{0}, {1}". format(, self.age)
>>> p1 = Person("niefajun", 100)
>>> p1
__repr __:niefajun, 100
>>> print(p1)
__str __:niefajun, 100

Procedure description:

  1. In the command line environment, define the class Person, which contains two instance attributes name and age, and additionally rewrites the two methods__str __(self)and__repr __(self), and the instance Object p1
  2. When executing >>> p1 alone, because of the command line environment, this time will execute__repr __(self)to complete the printing function, so it displays: __repr __:niefajun, 100, and when using print(p1) is equivalent to showing to the user, it will call the __str __(self) method, so the output: __str __:niefajun, 100`.

Note:All used in python files are __str__

For more exciting blogs, please visit: Nie Fajun's Technical Blog
For the corresponding video tutorial, please visit: python400
For full markdown notes, please visit: python400 \ _learn \ _github