Understanding difference between Double Quote and Single Quote with __repr__()

后端 未结 3 794
天命终不由人
天命终不由人 2021-01-13 02:02

What is the difference between print, object, and repr()? Why is it printing in different formats?

See the output differ

3条回答
  •  走了就别回头了
    2021-01-13 02:23

    See __repr__:

    Called by the repr() built-in function and by string conversions (reverse quotes) to compute the “official” string representation of an object. If at all possible, this should look like a valid Python expression that could be used to recreate an object with the same value (given an appropriate environment).

    And __str__:

    Called by the str() built-in function and by the print statement to compute the “informal” string representation of an object. This differs from __repr__() in that it does not have to be a valid Python expression: a more convenient or concise representation may be used instead.

    Emphasis added by me.

提交回复
热议问题