[Tutor] More Doubt with classes

Carroll, Barry Barry.Carroll at psc.com
Tue Jan 24 20:04:29 CET 2006


Greetings:

I took Edgar's script and added some more people.  Here is the script as
I ran it.  

========================================
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.'''
        print self.name + ' says: ',
        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()

butch = Person('Butch Cassidy')
butch.sayHi()
butch.howMany()

gerald = Person('Gerald McBoingBoing')
gerald.sayHi()
gerald.howMany()

tony = Person('Tony Danza')
tony.sayHi()
tony.howMany()

swaroop.howMany()
kalam.howMany()
butch.howMany()
gerald.howMany()
tony.howMany()
========================================

When I ran it on my system (Windows XP Professional), I got an error as
the script was cleaning up.  Here is the output.  

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
(Initializing Swaroop)
Hi, my name is Swaroop.
Swaroop says:  I am the only person here.
(Initializing Abdul Kalam)
Hi, my name is Abdul Kalam.
Abdul Kalam says:  We have 2 persons here.
(Initializing Butch Cassidy)
Hi, my name is Butch Cassidy.
Butch Cassidy says:  We have 3 persons here.
(Initializing Gerald McBoingBoing)
Hi, my name is Gerald McBoingBoing.
Gerald McBoingBoing says:  We have 4 persons here.
(Initializing Tony Danza)
Hi, my name is Tony Danza.
Tony Danza says:  We have 5 persons here.
Swaroop says:  We have 5 persons here.
Abdul Kalam says:  We have 5 persons here.
Butch Cassidy says:  We have 5 persons here.
Gerald McBoingBoing says:  We have 5 persons here.
Tony Danza says:  We have 5 persons here.
Butch Cassidy says bye.
There are still 4 people left.
Abdul Kalam says bye.
There are still 3 people left.
Gerald McBoingBoing says bye.
There are still 2 people left.
Swaroop says bye.
There are still 1 people left.
Tony Danza says bye.
Exception exceptions.AttributeError: "'NoneType' object has no attribute
'population'" in <bound method Person.__del__ of <__main__.Person
instance at 0x00909BC0>> ignored
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

Surprisingly, when I comment out the statements referencing the 'tony'
instance, the error goes away.

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
(Initializing Swaroop)
Hi, my name is Swaroop.
Swaroop says:  I am the only person here.
(Initializing Abdul Kalam)
Hi, my name is Abdul Kalam.
Abdul Kalam says:  We have 2 persons here.
(Initializing Butch Cassidy)
Hi, my name is Butch Cassidy.
Butch Cassidy says:  We have 3 persons here.
(Initializing Gerald McBoingBoing)
Hi, my name is Gerald McBoingBoing.
Gerald McBoingBoing says:  We have 4 persons here.
Swaroop says:  We have 4 persons here.
Abdul Kalam says:  We have 4 persons here.
Butch Cassidy says:  We have 4 persons here.
Gerald McBoingBoing says:  We have 4 persons here.
Butch Cassidy says bye.
There are still 3 people left.
Abdul Kalam says bye.
There are still 2 people left.
Gerald McBoingBoing says bye.
There are still 1 people left.
Swaroop says bye.
I am the last one.
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

I can't figure out what the difference is.  Does anyone have an idea?  

Regards,
 
Barry

PS Sorry for the long post.  I didn't want to leave anything out that
might contain a clue.
BGC
 
> -----Original Message-----
> Date: Fri, 20 Jan 2006 20:20:29 -0600
> From: Edgar Antonio Rodr?guez Velazco
<edgar.antonio.rv at gmail.com>
> Subject: [Tutor] Doubt with classes
> To: tutor at python.org
> Message-ID:
> 	<9378d12c0601201820nce4b1c0w1fad545f33976a82 at mail.gmail.com>
> Content-Type: text/plain; charset="iso-8859-1"
> 
> 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:
> 
> #############################################
> <snip> 
> ##################################
> 
> 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
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL:
>
http://mail.python.org/pipermail/tutor/attachments/20060120/c527657b/att
ac
> hment.htm




More information about the Tutor mailing list