str()和repr()的区别

class P(object):
    def ff(self):
        print(self.__mapper__)

    def __str__(self):
        return "str..."

    def __repr__(self):
        return "repr..."

if __name__ == '__main__':
    p = P()
    print(p)
    print(str(p))
    print(repr(p))
  • str()函数实际上就是调用对象的__str__()方法
  • repr()函数实际上就是调用对象的__repr__()方法

字符串的str和repr返回值的区别:

repr返回的是原始字符串,转义字符原样输出

str则会进行转义

posted @ 2021-04-07 16:47  foreast  阅读(131)  评论(0编辑  收藏  举报