免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3723 | 回复: 5
打印 上一主题 下一主题

基础问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-12-13 15:30 |只看该作者 |倒序浏览
不才是python新手,看着《A BYTE OF PYTHON》示例练习,其中十一章面向对象部分有代码如下:

#!/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 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

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

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

swaroop.sayHi()
swaroop.howMany()


可正常运行,但是假如在实例化时将其中的对象名比如swaroop改成xswaroop即xswaroop = Person('Swaroop'),出现一下情况:
Exception exceptions.AttributeError: "'NoneType' object has no attribute 'population'" in <bound method Person.__del__ of <__main__.Person instance at 0xb7d64d6c>> ignored
思考不得其解,还望赐教~~

[ 本帖最后由 knight0450 于 2008-12-13 15:33 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2008-12-13 15:57 |只看该作者
瞎说。
xswaroop = Person('Swaroop')
xswaroop.sayHi()
xswaroop.howMany()

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

xswaroop.sayHi()
xswaroop.howMany()
我测试正常,你把你修改后出错的代码帖出来。

论坛徽章:
0
3 [报告]
发表于 2008-12-13 16:54 |只看该作者
#!/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 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

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

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

xswaroop.sayHi()
xswaroop.howMany()


运行结果

(Initializing Swaroop)
Hi, my name is Swaroop.
I am the only person here.
(Initializing Abdul Kalam)
Hi, my name is Abdul Kalam.
We have 2 persons here.
Hi, my name is Swaroop.
We have 2 persons here.
Abdul Kalam says bye.
There are still 1 people left.
Swaroop says bye.
Exception exceptions.AttributeError: "'NoneType' object has no attribute 'population'" in <bound method Person.__del__ of <__main__.Person instance at 0xb7ddfd6c>> ignored

论坛徽章:
0
4 [报告]
发表于 2008-12-13 16:59 |只看该作者
2.5.1
2.5.2
2.6.1
三台机子不同版本都试过了啊

论坛徽章:
0
5 [报告]
发表于 2008-12-14 10:07 |只看该作者
这个问题是由于Python不保证对象的析构顺序造成的
比如改成swaroo = Person('Swaroop')不会有问题
或者显式del就没问题了
xswaroop = Person('Swaroop')
xswaroop.sayHi()
xswaroop.howMany()
del  xswaroop

论坛徽章:
0
6 [报告]
发表于 2011-06-22 16:51 |只看该作者
谢谢谢谢,正好也碰到了这个问题,解决了
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP