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

后端 未结 7 1898
名媛妹妹
名媛妹妹 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:45

    >>> from datetime import date
    >>>
    >>> repr(date.today())        # calls date.today().__repr__()
    'datetime.date(2009, 1, 16)'
    >>> eval(_)                   # _ is the output of the last command
    datetime.date(2009, 1, 16)
    

    The output is a string that can be parsed by the python interpreter and results in an equal object.

    If that's not possible, it should return a string in the form of <...some useful description...>.

提交回复
热议问题