例子的链接:
http://www.woodpecker.org.cn:9081/doc/abyteofpython_cn/chinese/ch11s06.html
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()
运行起来完全没有问题
但如果把测试那一段改为:
test=Person('Swaroop')
test.sayHi()
test.howMany()
kalam=Person('Abdul Kalam')
kalam.sayHi()
kalam.howMany()
test.sayHi()
test.howMany()
运行的时候就会得到一个异常:
Exception exceptions.AttributeError: "'NoneType' object has no attribute 'popula
tion'" in <bound method Person.__del__ of <__main__.Person instance at 0x00BEC12
0>> ignored
然后改很多次那个变量都有这个异常,好像那个变量只有用swaroop才没有问题,非常怪异。。。。
有没有人知道这是怎么回事?
我用的python版本: Python 2.5.2 windows下面的。
分享到:
- 2008-08-24 09:30
- 浏览 937
- 评论(1)
- 论坛回复 / 浏览 (1 / 2372)
- 查看更多
相关推荐
简明python教程里面的例子,自己运行的可执行的实例
Python简明教程及例子代码, 基于Python3.0或以后版本
10. 解决问题——编写一个Python脚本 问题 解决方案 版本一 版本二 版本三 版本四 进一步优化 软件开发过程 概括 11. 面向对象的编程 简介 self 类 创建一个类 对象的方法 使用对象的方法 __init__...
Python简明教程,内容简单精炼,例子突出典型,非常适合刚入门的朋友
10. 解决问题——编写一个Python脚本 问题 解决方案 版本一 版本二 版本三 版本四 进一步优化 软件开发过程 概括 11. 面向对象的编程 简介 self 类 创建一个类 对象的方法 使用对象的方法 _...
简明Python教程,从菜鸟级别逐渐进阶,了解Python语言的基本语法,以及跟着例子做一些实践,可以增加对语言的了解
入门必备教材,初学必备,其中例子较为简单,可以一边看书一边练习
一本Python初学者入门的好书,编者语言活泼,例子简单明了,重点提示深得精髓!
10. 解决问题——编写一个Python脚本 问题 解决方案 版本一 版本二 版本三 版本四 进一步优化 软件开发过程 概括 11. 面向对象的编程 简介 self 类 创建一个类 对象的方法 使用对象的方法 _...
简明python上的例子,python新手写的,仅供参考
第一个Python程序 Python 简介 Python 的安装 第一个 Python 程序 集成开发环境(IDE): PyCharm 基本数据类型和变量 Python 语法的简要说明 print() 函数 Python 的基本数据类型 字符串的编码问题 基本数据类型转换...
还是简明Python教程,含大多数例子的3.1版源码,不用积分的~
本文实例讲述了Python简明入门教程。分享给大家供大家参考。具体如下: 一、基本概念 1、数 在Python中有4种类型的数——整数、长整数、浮点数和复数。 (1)2是一个整数的例子。 (2)长整数不过是大一些的整数。 ...
主要介绍了Python中尝试多线程编程的一个简明例子,由于GIL的存在,Python中的多线程编程一个是热点和难点问题,需要的朋友可以参考下
书中除了给出一些以游戏和数学为主的典型实例外,还在每章的开始使用简单的图形给出一两个例子,以激发学生的学习兴趣。 本书特色 以“基础先行”方法介绍基本程序设计概念和方法,帮助学生循序渐进地学习所有...
主要介绍了Python操作SQLite简明教程,简单明了的入门教程,包含连接、建表、增删修查等例子,需要的朋友可以参考下
给的都是简明python教程上那个一长串代码的例子,说看了就明白了,可我看得晕头转向的还是没真正明白,只知道“sys.argv[0]表示代码本身文件路径”这点,其实还是不明其意。后来经过大量努力,多方求教才真正明悟了...