Detailed introduction to the execution scheme of Python programs

Source: Internet
Author: User

This article is mainly to track the execution of a relatively simple Python program, to briefly discuss the introduction of the basic framework and structure of Python implementation. The following describes the specific application of the actual operation solution for Python program execution.

To execute the Python program as follows, the function is very simple: Add 1 to 10 and print it out.

 
 
  1. # test program   
  2. sum = 0   
  3. for i in range(1, 11):   
  4. sumsum = sum + i   
  5. print sum   

If you want to debug Python with VS 2005 in Windows, you can set the Startup Project to Python through the following steps: Right-click the Python Project through F5, select Properties. In the dialog box

 
 
  1. Configuration Properties->Debugging 

Next, SET Command Arguments to-d test. py. Test. py is the name of the program to be debugged. -D indicates that the debug switch is enabled and additional debugging information is displayed.
Well, after setting, you can directly press F10 to track the execution of the program in a single step.

First, F10, start the program, and you can see that there is nothing in the main function of Python, just a simple call to Py_Main. Py_Main, as its name implies, is naturally the main function, which is divided into several parts:

Analyze command lines and Environment Variables

Call Py_Initialize for initialization

Enter different execution modes based on the command line content.

 
 
  1. if (command) {   
  2. sts = PyRun_SimpleStringFlags(command, &cf) != 0;   
  3. free(command);   
  4. } else if (module) {   
  5. sts = RunModule(module);   
  6. free(module);   
  7. }   
  8. else {   
  9. if (filename == NULL && stdin_is_interactive) {   
  10. RunStartupFile(&cf);   
  11. }   
  12. /* XXX */   
  13. sts = PyRun_AnyFileExFlags(   
  14. fp,   
  15. filename == NULL ? "<stdin>" : filename,   
  16.  
  17. filename != NULL, &cf) != 0;   
  18.  
  19. }    

The above is an introduction to the execution process of a simple Python program.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.