How to better run the Python Interpreter

Source: Internet
Author: User

If you suddenly exit the Python interpreter and then enter, the original functions and variables will be lost. Therefore, if you need to write a longer program, it is best to use a text editor to prepare input for the interpreter to reduce the occurrence of some errors.

Run the Python interpreter using the program file as input, which is called the script preparation ). When your program grows, it is best to split it into several files for maintenance. You may also want to use a very convenient function in several programs.

But I don't want to assign the function definition to every program. To support this, Python has a way to put the definition in a file and then call it in a script or interactive operation. Such a file is called a module;

The definition in the module can be imported to other modules or the main module of the main module refers to the set of variables that can be accessed by scripts or interactive programs that are executed at the top level of the interpreter ). A module contains Python definitions and statements. The file name consists of the module name and the suffix ". py. In the module, the module name can be used as a string.) The value of the global variable _ name _ is known.

For example, in the Python search path, Python 1.5.2, the text editor you are used to use, contains an IDLE integrated development environment written by Tkinter, MS Windows has a PythonWin interface can also be edited Python program) generate a name named. py file, including the following content:

 
 
  1. # Fibonacci numbers module
  2. Def fib (n): # the output of the fiber-ACCI sequence smaller than n
  3. A,B=0, 1
  4. While B< N: 
  5. Print B,
  6. A,BB= B, a + B
  7. Def fiber 2 (n): # returns a sequence of less than n.
  8. Result= []
  9. A,B=0, 1
  10. While B< N: 
  11. Result. append (B)
  12. A,BB= B, a + B
  13. Return result

Then, enter the Python interpreter. in IDLE or PythonWin, you can directly enter the interpreter window). Run the following command to import the module:

 
 
  1. >>> fibo.fib(1000)  
  2. 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987  
  3. >>> fibo.fib2(100)  
  4. [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]  
  5. >>> fibo.__name__  
  6. 'fibo'  

This does not directly introduce the function name in the module fibo to the current symbol table, but only introduces the module name fibo. You can use the module name to access the functions:

 
 
  1. >>>From fiber import fib, fiber 2
  2. >>>Fib (500)
  3. 1 1 2 3 5 8 13 21 34 55 89 144 233
  4.  
  5. This will not import the module name into the user's symbol table. For example, in the above example, fibo is not defined ).
  6.  
  7. There is also a way to import all the names defined in a module:
  8.  
  9. >>>From fibo import *
  10. >>>Fib (500)
  11. 1 1 2 3 5 8 13 21 34 55 89 144 233

In addition to function definitions, a module can contain executable statements. These executable statements are used to initialize the module. They are only executed when the module is imported for the first time. Each module has its own private symbol table, which is a global symbol table for all functions in the module.

Therefore, the module Author can use global variables in the module without worrying about conflicts with global variables of the module users. On the other hand, if you are sure, you can use the function format in the access module, that is, the modname. itemname method, to modify the global variables in the module. Modules can be imported into other modules. We usually place all the import statements at the beginning of a module or script. This is not required. The imported Module name is placed in the global symbol table of the module.

  1. Introduction to Python system files
  2. How to correctly use Python Functions
  3. Detailed introduction and analysis of Python build tools
  4. Advantages of Python in PythonAndroid
  5. How to Use the Python module to parse the configuration file?

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.