[Tutor] Doubt with classes

Alan Gauld alan.gauld at freenet.co.uk
Sat Jan 21 10:21:36 CET 2006


swaroop = Person('Swaroop')
swaroop.sayHi()
swaroop.howMany()

kalam = Person('Abdul Kalam')
kalam.sayHi()
kalam.howMany()

swaroop.sayHi()
swaroop.howMany()

#################################
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???

The script never calls del() so it is relying on Garbage Collection
to kill the objects.

How are you running the script? From a command line in the OS or
by importing it into a Python session? If you only import it then you will
get the result you see. You will need to run the script by
1) Double clicking in a File broewsrer(like Windows explorer) or
2) From a console window typing "ython scripy.py"

Alternarively, add
del(swaroop)
del(kalam)

to the end of your script...


HTH,

Alan G
Author of the learn to program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld




More information about the Tutor mailing list