Discuss / Python / 实现添加时count+1,删除时count-1

实现添加时count+1,删除时count-1

Topic source

岁益寒

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

# -*- coding: utf-8 -*-

class Student(object):

    count = 0

    # count += 1

    def __init__(self, name):

        self.name = name

        Student.count += 1

    def __del__(self):

        Student.count -= 1

print(Student.count == 0)

bart = Student('Bart')

print(Student.count == 1)

auther = Student('Auther')

print(Student.count == 2)

del auther

print(Student.count == 1)


  • 1

Reply