On the other hand, if you know what you are doing you can touch a module’s global variables with the same notation used to refer to its functions, emname. Thus, the author of a module can use global variables in the module without worrying about accidental clashes with a user’s global variables. 模块包含可执行语句和函数定义。这些语句用于初始化模块,他们仅仅在模块第一次导入时被执行。Įach module has its own private symbol table, which is used as the global symbol table by all functions defined in the module.
![text spammer program yo yo yo yo yo yo yo yo yo yo yo yo yo yo text spammer program yo yo yo yo yo yo yo yo yo yo yo yo yo yo](https://www.ggaub.com/yo/images/yoyo_13.jpg)
(They are also run if the file is executed as a script.) They are executed only the first time the module name is encountered in an import statement. These statements are intended to initialize the module. For instance, use your favorite text editor to create a file called fibo.py in the current directory with the following contents: Within a module, the module’s name (as a string) is available as the value of the global variable _name_. The file name is the module name with the suffix. Such a file is called a module definitions from a module can be imported into other modules or into the main module (the collection of variables that you have access to in a script executed at the top level and in calculator mode). To support this, Python has a way to put definitions in a file and use them in a script or in an interactive instance of the interpreter. You may also want to use a handy function that you’ve written in several programs without copying its definition into each program. As your program gets longer, you may want to split it into several files for easier maintenance. Therefore, if you want to write a somewhat longer program, you are better off using a text editor to prepare the input for the interpreter and running it with that file as input instead. If you quit from the Python interpreter and enter it again, the definitions you have made (functions and variables) are lost.