同一python程序,为何在IDLE和PyCharm上运行,会得到两种结果

py_66 2017-09-01 11:57:12
同一程序,在IDLE和PyCharm上运行结果如下:


附代码:
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()
...全文
676 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
py_66 2017-09-01
  • 打赏
  • 举报
回复
引用 1 楼 xpresslink 的回复:
在IDLE中执行时因为解释器没有关闭所以没有触发 __del__ 在最下面加两句就一样了。 del(kalam) del(swaroop)
嗯,现在一样了,谢谢
混沌鳄鱼 2017-09-01
  • 打赏
  • 举报
回复
在IDLE中执行时因为解释器没有关闭所以没有触发 __del__ 在最下面加两句就一样了。 del(kalam) del(swaroop)

37,720

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧