【3】

'''
3. Define a variable in a class
to track how many instances of the class are created (when an object is instantiated, this variable is +1,
when an object is destroyed, this variable is automatically -1)
'''

class Person:
    count=0
    def __init__(self):
        Person.count+=1
    def __del__(self):
        Person.count-=1
p=Person()
print(Person.count)
del p
print(Person.count)

Navigation Bar

Guess you like

Origin blog.csdn.net/qq_43586192/article/details/111834252
138
233
234