Edgar Antonio RodrÃguez Velazco wrote: > Hi everybody, > I've been reading the chapter of classes of Byte of Python by Swaroop. > There's an example with classes (11.4) that is below:
The example is depending on Person.__del__() being called on swaroop and kalam when the intepreter exits. The Python Language Reference documentation on __del__() says, "It is not guaranteed that __del__() methods are called for objects that still exist when the interpreter exits." So this program is relying on behaviour that may change between different versions of Python. Try adding del swaroop del kalam to the end of the program, this should force the __del__() method to be called (at least in CPython, in Jython it still may not be called). Kent > > ############################################# > class Person: > '''Represents a person.''' > population = 0 > > def __init__(self, name): > '''Initializes the person's data.''' > self.name = name > print '(Initializing %s)' % self.name > > # When this person is created, he/she > # adds to the population > Person.population += 1 > > def __del__(self): > '''I am dying.''' > print '%s says bye.' % self.name > > Person.population -= 1 > > if Person.population == 0: > print 'I am the last one.' > else: > print 'There are still %d people left.' % Person.population > > def sayHi (self): > '''Greeting by the person. > > Really, that's all it does.''' > print 'Hi, my name is %s.' % self.name > > def howMany(self): > '''Prints the current population.''' > if Person.population == 1: > print 'I am the only person here.' > else: > print 'We have %d persons here.' % Person.population > > swaroop = Person('Swaroop') > swaroop.sayHi() > swaroop.howMany() > > kalam = Person('Abdul Kalam') > kalam.sayHi() > kalam.howMany() > > swaroop.sayHi() > swaroop.howMany() > > ################################# > EXAMPLE'S OUTPUT (Output shown in the example) > > $ python objvar.py > (Initializing Swaroop) > Hi, my name is Swaroop. > I am the only person here. > (Initializing Abdul Kalam) > Hi, my name is Abdul Kalam. > We have 2 persons here. > Hi, my name is Swaroop. > We have 2 persons here. > Abdul Kalam says bye. > There are still 1 people left. > Swaroop says bye. > I am the last one. > ############################## > MY OUTPUT > > (Initializing Swaroop) > Hi, my name is Swaroop. > I am the only person here. > (Initializing Abdul Kalam) > Hi, my name is Abdul Kalam. > We have 2 persons here. > Hi, my name is Swaroop. > We have 2 persons here. > ################################## > > I have runned the script in both Linux and Windows and got the same > result. Could you explain me what's wrong with this??? > > -- > Edgar A. Rodriguez > > > ------------------------------------------------------------------------ > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor