标题:请帮助找一下问题在哪里。Python初学
只看楼主
duweix
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2011-8-25
结帖率:0
已结贴  问题点数:0 回复次数:1 
请帮助找一下问题在哪里。Python初学
我使用的是Python 2.7 (r27:82500, Sep 16 2010, 18:03:06),系统是Fedora 14。
程序执行时除了一些正常的输出外,最后还报告以下错误:
Exception AttributeError: "'NoneType' object has no attribute 'population'" in <bound method Person.__del__ of <__main__.Person instance at 0xb77357cc>> ignored
请大家帮看看是哪里有问题呢?谢谢!
以下是程序源代码(代码来源于《简明Python教程》http://):
程序代码:
#!/usr/bin/python
# filename : objvar.py

class Person:
    '''Represents a person.'''
    population = 0

    def __init__(self, name):
        '''Initializes the person' data.'''
        self.name = name
        print '(Initializing %s)' % self.name

        # When this person 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 persons here.' % Person.population

duwei = Person('duwei')
duwei.sayHi()
duwei.howMany()

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

duwei.sayHi()
duwei.howMany()


搜索更多相关主题的帖子: 2010 源代码 method 
2011-08-25 15:52
外部三电铃
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:那一年
等 级:贵宾
威 望:55
帖 子:2004
专家分:7306
注 册:2007-12-17
得分:20 
__del__是执行销毁任务的,也就是说Person已经被销毁了,当然再调用Person.population就会出错了,可能在以前的版本不出错,但我的2.5版本会出错,2.5以后的应该都会提示出错

那一年,苍井空还是处女
2011-08-25 22:26



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-348398-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.359217 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved