Python object.__repr__(self) should be an expression?

后端 未结 7 1913
名媛妹妹
名媛妹妹 2020-12-22 15:36

I was looking at the builtin object methods in the Python documentation, and I was interested in the documentation for object.__repr__(self). Here\'s what it sa

7条回答
  •  醉酒成梦
    2020-12-22 15:36

    'repr' means represention. First,we create an instance of class coordinate.

    x = Coordinate(3, 4) 
    

    Then if we input x into console,th output is

    <__main__.Coordinate at 0x7fcd40ab27b8>
    

    If you use repr():

    >>> repr(x)
    Coordinate(3, 4)
    

    the output is as same as 'Coordinate(3, 4)',except it is a string.You can use it to recreate a instance of coordinate.

    In conclusion,repr() meathod is print out a string,which is the representation of the obeject.

提交回复
热议问题