python之__str__用法

class Person(object):
    def __init__(self,name,gender):
        self.name = name
        self.gender = gender

    def __str__(self):
        return ‘(Person: %s, %s)‘ %(self.name,self.gender)
    __repr__ = __str__
p = Person(‘Bob‘,‘male‘)
#p = Person()
print(p)


class Persons(object):
    def __init__(self,name,gender):
        self.name = name
        self.gender = gender

class Student(Persons):
    def __init__(self,name,gender,score):
        super(Student, self).__init__(name,gender)
        self.score = score

    def __str__(self):
        return ‘(Student: %s, %s, %s)‘ %(self.name,self.gender,self.score)

s = Student(‘zou‘,‘nan‘,‘80‘)
print(s)