class TestStrRepr(object):
    def __str__(self):
        return "good, this is TestStr"  #必须返回字符串
  print TestStr() #good, this is TestStr

可以认为__str__的目的是为print 这样的打印函数调用而设计的,当print 一个对象时,会自动调用其__str__方法

而repr 函数则是将对象转换为字符串显示

a = "hello"

repr(a) # "'hello'"

repr([1,2,3]) #'[1,2,3]'

相关文章:

  • 2022-12-23
  • 2021-09-16
  • 2022-12-23
  • 2022-12-23
  • 2022-02-24
  • 2022-03-10
  • 2022-12-23
  • 2023-01-25
猜你喜欢
  • 2022-01-09
  • 2022-12-23
  • 2021-10-19
  • 2021-07-30
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案