overriding built-in __str__() method
class Employee:
def __init__(self, lastname, firstname=None):
self.lastname = lastname
self.firstname = firstname
def __str__(self):
if self.firstname:
return "%s %s" % (self.firstname, self.lastname)
else:
return self.lastname
Related examples in the same category