python 在打印一个实例化对象时,打印的是对象的地址,比如:<__main__.Workers object at 0x00000000255A9AC8>

而__str__(self)就可以打印具体的属性,用法如下:

def __str__(self):
        msg = "姓名:{},工号:{},工资:{}".format(self.name,self.number,self.salary)
        return msg

在打印实例化对象时打印的内容:

姓名:**,工号:0001,工资:3600

相关文章:

  • 2021-12-31
  • 2022-03-05
  • 2021-12-01
  • 2022-01-16
  • 2022-12-23
  • 2022-12-23
  • 2023-01-25
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-09-01
  • 2021-11-09
  • 2021-09-09
  • 2022-12-23
  • 2022-01-09
相关资源
相似解决方案