Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Person(object):
- count = 0
- def __init__(self, name, age):
- klass = globals()[self.__class__.__name__]
- self.name = name
- self.age = age
- klass.count += 1
- def __del__(self):
- klass = globals()[self.__class__.__name__]
- klass.count -= 1
- @classmethod
- def get_count(cls):
- return cls.count
- class Student(Person):
- def __init__(self, name, age):
- klass = globals()[self.__class__.__name__]
- super(klass, self).__init__(name, age)
- for base in self.__class__.__bases__:
- parent = globals()[base.__name__]
- parent.count += 1
- def __del__(self):
- klass = globals()[self.__class__.__name__]
- super(klass, self).__del__()
- for base in self.__class__.__bases__:
- parent = globals()[base.__name__]
- parent.count -= 1
- if __name__ == '__main__':
- a = Student('Michael', 35)
- b = Person('Gladys', 52)
- c = Student('Susan', 26)
- print 'person:', Person.get_count()
- print 'student:', Student.get_count()
- del c
- print 'person:', Person.get_count()
- print 'student:', Student.get_count()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement