__str__ and __repr__ in Python

What's difference between __str__ and __repr__ ?

__str__ and __repr__

__str__

  • print(obj) 会调用__str__
  • 如果__str__没有被重写, 那么将调用__repr__

__repr__

  • 在交互式环境下, 直接输入对象, 打印会调用__repr__

Some Analysis

Condition 1

  • 只重写__str__
  • __repr__调用父类
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class Apple(object):

    def __repr__(self) -> str:
        return super().__repr__()

    def __str__(self) -> str:
        return f'This is a {self.__color} apple.[Print by __str__]'

    def __init__(self, color):
        self.__color = color

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
λ python
Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from base_knowledge import Apple
>>> red_apple = Apple("red")
>>> red_apple
<base_knowledge.Apple object at 0x000002BCE2110AF0>
>>> print(red_apple)
This is a red apple.[Print by __str__]
>>>

从以上输出,

  • print调用了__str__
  • 交互式环境,直接输出了地址信息

Condition 2

  • 只重写__repr__
  • __str__调用父类
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class Apple(object):

    def __repr__(self) -> str:
        return f'This is a {self.__color} apple.[Print by __repr__]'

    def __str__(self) -> str:
        return super().__str__()

    def __init__(self, color):
        self.__color = color

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
λ python
Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from base_knowledge import Apple
>>> red_apple = Apple("red")
>>> red_apple
This is a red apple.[Print by __repr__]
>>> print(red_apple)
This is a red apple.[Print by __repr__]
>>>

从以上输出,

  • 交互式环境, 调用了重写的__repr__
  • print也调用了__repr__

Condition 3

  • 重写__str__
  • 重写__repr__
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class Apple(object):

    def __repr__(self) -> str:
        return f'This is a {self.__color} apple.[Print by __repr__]'

    def __str__(self) -> str:
        return f'This is a {self.__color} apple.[Print by __str__]'

    def __init__(self, color):
        self.__color = color

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
λ python
Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from base_knowledge import Apple
>>> red_apple = Apple("red")
>>> red_apple
This is a red apple.[Print by __repr__]
>>> print(red_apple)
This is a red apple.[Print by __str__]
>>>

从以上输出,

  • 交互式环境, 直接输出, 调用了__repr__
  • print, 调用了__str__

Condition 4

  • 都没重写
1
2
3
4
class Apple(object):

    def __init__(self, color):
        self.__color = color

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
λ python
Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from base_knowledge import Apple
>>> red_apple = Apple("red")
>>> red_apple
<base_knowledge.Apple object at 0x0000025ABFE10AF0>
>>> print(red_apple)
<base_knowledge.Apple object at 0x0000025ABFE10AF0>
>>>

结合Condition1, 2, 3, 4, 可以得出以下结论

  • __str__如果没有被重写, 默认调用__repr__
  • 交互式环境下, 直接输出, 一定调用__repr__(重写, 就调用重写的方法; 未重写, 就调用父类的)
Licensed under CC BY-NC-SA 4.0
Last updated on Jun 07, 2020 22:47 UTC