只要定义了__str__(self)方法,那么就会打印从这个方法中return的数据

 

class Car:
def __init__(self, newWheelNum, newColor):
self.wheelNum = newWheelNum
self.color = newColor

def __str__(self):
msg = "My color:" + self.color + ", my wheel num:" + str(self.wheelNum)
return msg

def move(self):
print "Car is running ..."

BMW = Car(4, "White")
print BMW

输出:My color:White, my wheel num:4

 

如果将__str__()函数注释掉

print BMW的输出是:

<__main__.Car instance at 0x0000000003780C08>

 

相关文章:

  • 2022-12-23
  • 2021-11-02
  • 2022-01-13
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-07-30
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-24
  • 2022-12-23
  • 2021-12-28
相关资源
相似解决方案