免费注册 查看新帖 |

Chinaunix

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

求助 PyScripter调试问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-04-03 02:09 |只看该作者 |倒序浏览
代码:
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()


这是简明Python教程里的一个例子
我使用PyScripter调试的

2个问题
1.为什么我run第一次和第二次的结果是不同的
2.如何设置断点调试python程序

[ 本帖最后由 Tiger_cn 于 2008-4-5 02:36 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2008-04-05 02:40 |只看该作者
人气这么低?

论坛徽章:
0
3 [报告]
发表于 2008-04-05 12:01 |只看该作者
原帖由 Tiger_cn 于 2008-4-3 02:09 发表
代码:
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

...



对于两个问题
1 运行两次,结果不同的原因,在于标红的代码
population 是class Person的class attribute (在__init__函数中的attribute是data attribute).
class attribute是类本身的属性,在创建任何类实例之前就有效的,即可以通过对类本身进行引用,也可以通过类实例进行引用
class attribute的值作用域在于类,一旦在某个实例中被修改后,对类本身以及其它实例也有影响。
所以没有任何实例之前, person = 0,第一次执行时person=1,第二次person=2,依次递加。
如果调用__del__,运行次数不同,结果也应该不同的。

2 由于在win下面用的是PythonWin或eclipse,所以不太清楚PyScripter的调试技巧...汗  :wink:

论坛徽章:
0
4 [报告]
发表于 2008-04-05 13:58 |只看该作者
(Initializing Swaroop)
Swaroop says bye.
I am the last one.
Hi, my name is Swaroop.
We have 0 persons here.
(Initializing Abdul Kalam)
Abdul Kalam says bye.
I am the last one.
Hi, my name is Abdul Kalam.
We have 0 persons here.
Hi, my name is Swaroop.
We have 0 persons here.

2次运行之后  以后无论再怎么运行  结果都是这个了

看来是__del__方法的原因了
谁能解释一下__del__?

论坛徽章:
0
5 [报告]
发表于 2008-04-06 00:54 |只看该作者
test in unbuntu
i can't type Chinese

(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.
I am the last one.

论坛徽章:
0
6 [报告]
发表于 2008-04-07 21:39 |只看该作者
pyscripter是用delphi做出来的,所以在使用上和delphi很类似:
1、按f5(或者代码编辑区的最左边)设置断点;
2、f8、f7单步调试,
3、ctrl+f2重新初始化运行环境;问题一中,run第一次和第二次的结果不同。那试下这个。
......

论坛徽章:
0
7 [报告]
发表于 2008-04-09 00:27 |只看该作者
谢谢。

论坛徽章:
0
8 [报告]
发表于 2008-04-09 14:48 |只看该作者
这段代码创建的不是两个类,而是两个对象(swaroop和kalam)。这两个对象用的是两块独立的内存(不过用Python编程,不需要关心这些操作系统中的细节)。
那么,至于为什么在第二个对象中,变量population的值为2呢?
这是由于类自身(这个例子里面是类Person)有一块存储空间,其中存储了变量population。
而仔细阅读一下代码,可以发现,在程序中使用了Person.population,这种标记,表明这是一个“类的变量”,而非“对象的变量”。对类的变量Person.population的修改,会反映到每一个对象上(在这个例子里面是对象swaroop和kalam)。

你可以进一步试试,把程序中,我标红的这些Person.population替换成self.population。self.population这种标记,表明它是“对象的变量”。这样的话,第二个对象kalam中,变量population的值就变成1了。

希望我的解释,能够让你理解。
最后,谢谢你学习Python,祝你成功。

沈 洁元
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP