Discuss / Python / 如果不放init()中,每次都会初始化count,count得不到修改

如果不放init()中,每次都会初始化count,count得不到修改

Topic source

YOUTH

#1 Created at ... [Delete] [Delete and Lock User]

class Student(object):

    count = 0

    def __init__(self, name):

        self.name = name

        Student.count += 1

为什么不能写成 self.count+=1 呢?

写成self.count += 1的话,每创建一个实例对象,类属性count会被重置为0


  • 1

Reply