เมธอด __str__
__str__ เป็น Special Method คล้าย __init__ คืน Informal String Representation ของออบเจ็กต์ เหมาะสำหรับ Debugging
ตัวอย่าง
class Time:
def __str__(self):
return '%.2d:%.2d:%.2d' % (self.hour, self.minute, self.second)
เมื่อ print ออบเจ็กต์ Python จะเรียก __str__:
>>> time = Time(7, 36) >>> print time 07:36:00