Python日常小问题

class Student(object):
    def __init__(self,name):
        self.name = name
    def print_name(self):
        print self.name
    age = 0

class Teacher(object):
    def __init__(self,name,zhuanye):
        self.name = name
        self.zhuanye = zhuanye
    def teachClass(self,students):
        print 'teacher name %s ' % self.name
        for s in students:
            print s.print_name()
            
if __name__ == '__main__':
    Student.age = 22
    print Student.age

    t = Teacher('haha','match')
    t.teachClass([Student('ran'),Student('dai'),Student('zhutou')])

打印结果:
22
teacher name haha
ran
None
dai
None
zhutou
None
闲来无事,有重温一下python中类这一章节,于是自己写了一个小demo,
但是突然发现多了几个None,于是就很好奇了。
仔细看过代码发现:
Teacher类中多了一个print
print s.print_name()
仔细一想print_name是无返回类型,所以默认返回的是None,还思考了半天,别自己蠢哭了

你可能感兴趣的:(Python日常小问题)