Python开发的初实践与用IDLE调试

来源:互联网 发布:几米 微笑的鱼 知乎 编辑:程序博客网 时间:2024/06/05 19:09

         由于需要,开始实际动手Python程序的编写,但是折腾半天,现在有种感慨——要了解其运行机制,从最开始一步一步跟踪其执行过程开始(以小程序试起,先从网上找代码看看),以下是用IDLE调试的步骤:

        1、先用IDLE中写入完整源码,我的代码如下:

*-coding:utf-8 -*-'''    figure the surface and volumn of cube'''class cube:    #注意,如果像下面这样写一些属性的话,那么,这些属性是作为类共享的,就像C++中的静态变量    def __init__(self,):        self.type=int        self.surface=long    def __init__(self,l,w,h):        self.l=l        self.w=w        self.h=h    def surface(self):        l=self.l        w=self.w        h=self.h        result=(l*w+l*h+w*h)*2        print 'the surface of cube is' + str(result)        return result    def volumn(self):        l=self.l        w=self.w        h=self.h        result=l*w*h        #result=L*W*H 奇怪的是,这个L\W\H通过调试可看到其在程序这里也可见,但却又说其未定义        print 'the volumn of cube is' + str(result)        return resultdef main():    L=raw_input('Plz input the length of the cube:')    L=int(L)    W=raw_input('Plz enter the width of the cube:')    W=int(W)    H=raw_input('Plz type in the height of the cube:')    H=int(H)    cube1=cube(L,W,H)    cube1.surface()    cube1.volumn()    

          2、编辑保存之后,点击Run->Python Shell,就打开了Python Shell 窗口,在这个窗口菜单上,选择Debug->Debuger,这时就打开了Debug Control窗口

         3、这时,再在IDLE那个源码窗口中点击Run->Run Module或按F5键,这时,就可以在Debug Control窗口里就看到了正要调试运行的程序的__main__模块被选中,如下图:


         点击上面的Step按钮,就可以看到其一步一步的执行过程,从这个过程中,我们还可以看到,解释器是从上往下一个模块一个模块的扫描,它先找到名字为'__main__'中的模块,然后从其下面调用的函数处去执行


原创粉丝点击