You just need to provide the __str__ method, and it will be called whenever something should display your object as a string.
For example, consider the following class:
class rrule: def __init__ (self): self.data = "" def schedule (self, str): self.data = str def __str__ (self): if self.data.startswith("d"): return "Daily, %s" % (self.data[1:]) if self.data.startswith("m"): return "Monthly, %s of the month" % (self.data[1:]) return "Unknown"
which prints itself nicely with the __str__ method. When you run the following code for this class:
xyzzy = rrule() print (xyzzy) xyzzy.schedule ("m3rd") print (xyzzy) xyzzy.schedule ("d4:30pm") print (xyzzy)
you will see the following output:
Unknown Monthly, 3rd of the month Daily, 4:30pm
source share