分享

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

 LibraryPKU 2015-11-23

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

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

  1. *-coding:utf-8 -*-  
  2. ''''' 
  3.     figure the surface and volumn of cube 
  4. '''  
  5. class cube:  
  6.     #注意,如果像下面这样写一些属性的话,那么,这些属性是作为类共享的,就像C++中的静态变量  
  7.     def __init__(self,):  
  8.         self.type=int  
  9.         self.surface=long  
  10.     def __init__(self,l,w,h):  
  11.         self.l=l  
  12.         self.w=w  
  13.         self.h=h  
  14.   
  15.     def surface(self):  
  16.         l=self.l  
  17.         w=self.w  
  18.         h=self.h  
  19.         result=(l*w+l*h+w*h)*2  
  20.         print 'the surface of cube is' + str(result)  
  21.         return result  
  22.   
  23.     def volumn(self):  
  24.         l=self.l  
  25.         w=self.w  
  26.         h=self.h  
  27.         result=l*w*h  
  28.         #result=L*W*H 奇怪的是,这个L\W\H通过调试可看到其在程序这里也可见,但却又说其未定义  
  29.         print 'the volumn of cube is' + str(result)  
  30.         return result  
  31.   
  32. def main():  
  33.     L=raw_input('Plz input the length of the cube:')  
  34.     L=int(L)  
  35.     W=raw_input('Plz enter the width of the cube:')  
  36.     W=int(W)  
  37.     H=raw_input('Plz type in the height of the cube:')  
  38.     H=int(H)  
  39.     cube1=cube(L,W,H)  
  40.     cube1.surface()  
  41.     cube1.volumn()  
  42.       
          2、编辑保存之后,点击Run->Python Shell,就打开了Python Shell 窗口,在这个窗口菜单上,选择Debug->Debuger,这时就打开了Debug Control窗口

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


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


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多