python学习笔记

阅读的是网上的文档 http://woodpecker.org.cn/abyteofpython_cn/chinese/ch11s06.html

这个是第十一章  类与对象的方法 

#!/usr/bin/python
# Filename : objvar.py
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 persion 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 person here.' % Person.population

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

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

swaroop.sayHi()
swaroop.howMany()

直接在IDLE里按F5执行,发现不执行__del__方法

截图如下

python学习笔记_第1张图片

然后我用命令行执行,发现都会调用介个方法 为什么呢

linux下:

python学习笔记_第2张图片

windows下:

python学习笔记_第3张图片

你可能感兴趣的:(python学习笔记)