37,743
社区成员




#!/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 '(Initializeing %s)' %self.name
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 say_hi (self):
'''Greeting by the person.'''
print 'Hi, my name is %s.' %self.name
def how_many (self):
if Person.population == 1:
print 'I am the only person here.'
else:
print 'We have %d persons here.' %Person.population
ljia = Person ('ljia')
ljia.say_hi ()
ljia.how_many ()
zhanzhao = Person ('zhanzhao')
zhanzhao.say_hi ()
zhanzhao.how_many ()
ljia.say_hi ()
ljia.how_many ()
(Initializeing ljia)
Hi, my name is ljia.
I am the only person here.
(Initializeing zhanzhao)
Hi, my name is zhanzhao.
We have 2 persons here.
Hi, my name is ljia.
We have 2 persons here.
ljia says bye.
There are still 1 people left
zhanzhao says bye.
Exception AttributeError: "'NoneType' object has no attribute 'population'" in <bound method Person.__del__ of <__main__.Person instance at 0xb76ad8ec>> ignored