Python的bug

Python太火了,不学点都感觉自己不是学计算机的,今天看了个不错的《简明python教程》,很不错。不过在学习过程中,居然发现了一个Python的bug,

 

#!/usr/bin/python #coding=UTF-8 class Person: '''Represents a person.''' population = 0 def __init__(self, name): '''Initializes the person's data.''' self.name = name print '(Initializing %s)' % self.name Person.population += 1 def __del__(self): '''I am dying''' print '%s syas 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 persons here.' % Person.population luolei = Person('luolei') luolei.sayHi() luolei.howMany() xiaoming = Person('Xiao Ming') xiaoming.sayHi() xiaoming.howMany() luolei.sayHi() luolei.howMany() 

 

Person在对象析构之前先析构了,在对象析构的时候找不到类的说明了。

 

避免这个bug的办法是在luolei前面加上一个_,即变成_luolei则没有问题,因为python会先析构前面有下划线的变量,如此NB的Python居然有这么个明显的问题,困惑

 

Python的版本:Python 2.6.4

你可能感兴趣的:(python,Class)