![]() ![]() You may have noticed that in this example, draw_game does not precede with the name of the module it is imported from, because we've specified the module name in the import command. ![]() We may also import the function draw_game directly into the main script's namespace, by using the from command. Importing module objects to the current namespace py file, but this process is transparent to the user. pyc file exists, it gets loaded instead of the. Python compiles files into Python bytecode so that it won't have to parse the files each time modules are loaded. pyc file appears, which is a compiled Python file. You may have noticed that when importing a module, a. If not, he will continue to look for built-in modules. py suffix, so in our case it will try to look for draw.py. When the import draw directive will run, the Python interpreter will look for a file in the directory which the script was executed from, by the name of the module with a. To reference the draw_game function from the game module, we would need to import the draw module and only then call draw.draw_game(). To use the function draw_game from the draw module, we would need to specify in which module the function is implemented, using the dot operator. The main function would use the local function play_game to run the game, and then draw the result of the game using a function implemented in the draw module called draw_game. In this example, the game module imports the draw module, which enables it to use functions implemented in that module. The draw module may look something like this: # draw.py # this means that if this script is executed, then main() will be executed In this example, the game.py script may look something like this: # game.py Modules are imported from other modules using the import command. It will use the function draw_game from the file draw.py, or in other words, the draw module, that implements the logic for drawing the game on the screen. The Python script game.py will implement the game. A Python module may have a collection of specified and implemented functions, classes or variables. The module's name would be the name of the file. Python modules are essentially Python files with an extension. Each module is a different file that can be separately edited. For example, one module would be in control of the game logic in creating a ping pong game, and another module would be in charge of drawing the game on the screen. A module in programming is a piece of software that has a particular functionality.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |