Thursday, 12 July 2018

Python __str__ Method

Cited from the book "Python How to Program"

A Python class can define special method __str__, to provide an informal (i.e., human-readable) string representation of an object of the class. If a client program of the class contains the statement

print objectOfClass

Python calls the object’s __str__ method and outputs the string returned by that method.

Returning a non-string value from method __str__ is a fatal, runtime error.


No comments:

Post a Comment