overriding built-in __str__() method : Class str « Class « Python






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

1.__str__ , __repr__ __str__ , __repr__
2.Override class __str__ functionOverride class __str__ function