Can someone here explain constructors and destructors in python - simple explanation required - new

Posted by rgolwalkar on Stack Overflow See other posts from Stack Overflow or by rgolwalkar
Published on 2010-03-12T13:53:05Z Indexed on 2010/03/12 14:27 UTC
Read the original article Hit count: 355

Filed under:
|
|

i will try to see if it makes sense :-

class Person:
    '''Represnts a person '''
    population = 0

    def __init__(self,name):
          //some statements and population += 1
    def __del__(self):
          //some statements and population -= 1 
    def sayHi(self):
        '''grettings from person'''
        print 'Hi My name is %s' % self.name

    def howMany(self):
        '''Prints the current population'''
        if Person.population == 1:
            print 'i am the only one here'
        else:
            print 'There are still %d guyz left ' % Person.population
rohan = Person('Rohan')
rohan.sayHi()
rohan.howMany()


sanju = Person('Sanjivi')
sanju.howMany()

del rohan # am i doing this correctly --- ? i need to get an explanation for this del - destructor

O/P:- 

Initializing person data
******************************************
Initializing Rohan
******************************************
Population now is: 1
Hi My name is Rohan
i am the only one here
Initializing person data
******************************************
Initializing Sanjivi
******************************************
Population now is: 2
In case Person dies: 
******************************************
Sanjivi Bye Bye world
there are still 1 people left
i am the only one here
In case Person dies: 
******************************************
Rohan Bye Bye world
i am the last person on earth
Population now is: 0

If required i can paste the whole lesson as well --- learning from :- http://www.ibiblio.org/swaroopch/byteofpython/read/

© Stack Overflow or respective owner

Related posts about python

Related posts about class