1 #! /usr/bin/python 2 # Filename:objvar.py 3 4 class Person: 5 '''Represents a person.''' 6 population = 0 7 8 def __init__(self, name): 9 ''' Initializes the person's data. ''' 10 self.name = name 11 print '(Initializeing %s)' % self.name 12 13 # When this person is created, he/she adds to the population 14 Person.population += 1 15 16 def __del__(self): 17 ''' I am dying. ''' 18 print '%s says bye.' % self.name 19 20 self.__class__.population -= 1 21 22 if self.__class__.population == 0: 23 print 'I am the last one.' 24 else: 25 print 'There are still %d people left.' % Person.population 26 27 def sayHi(self): 28 ''' Greeting by the person. 29 Really, that's all it does. ''' 30 print 'Hi, my name is %s.' % self.name 31 32 def howMany(self): 33 ''' Prints the current population. ''' 34 if Person.population == 1: 35 print 'I am the only person here.' 36 else: 37 print 'We have %d persons here.' % Person.population 38 39 # The definion of class is end 40 41 zjw = Person('zjw') 42 zjw.sayHi() 43 zjw.howMany() 44 45 lbz = Person('lbz') 46 lbz.sayHi() 47 lbz.howMany() 48 49 zjw.sayHi() 50 zjw.howMany()