If you have in your class "classes.py" something like:
import pygame
class Classe1(...):
...
then the main file:
from classes import *
Both the defined classes and the "pygame" that were imported into the classes.py file are imported into the main file.
The import
command brings variables defined in the modules - it does not matter if the variables are created directly with the "=" sign, or they are references to the functions, and therefore created with the keyword "def", classes, created with the word "class" or imported from another "beyond" module with the "import" command. A variable that exists in the "class1" module can be imported from another module, and the "pygame" variable that refers to the pygame package is no exception.
What happens is that it is not considered good practice to use from XXX import *
. This even breaks down the operation of various code analysis tools and various IDEs features.
You should import name by name of what you will use, which is defined in your "classes1" file, and do not use the "*". The use of "*" should only be done even in exceptions that confirm the rule that it should not be used. : -)
That said, you can write in your main module:
from classes1 import class1, class2, class3, pygame
But it's not recommended - it's best if you import Pygame as pygame into the main module, and the classes you want from your module, without using the "*":
import pygame
from classes1 import class1, class2, class3
What's important to keep in mind here is that Python will not "double-click" the pygame module. At runtime, it will simply see that the pygame has already been imported into another module, and create a variable in the current module to point to the same imported pygame module in the "classes1" module. The pygame files are not read more than once from the disk, no files are compiled more than once, and no pygame functionality will be present in duplicate memory.
So, in short - yes - the correct one in Python is to import into each module everything the code of that module will use, without depending on it being available in another module that you will also import. But there's no performance impact with that, just clarity that makes it easier to maintain your code.
Pygame in particular has a situation that was made to use import *
- the constants with the key and event codes of the mouse and joystick . They are available separately in module pygame.locals
.
Then you can do in your class module:
from pygame.locals import *
And have available to use the codes such as K_ESCAPE
, and etc ... and by importing only the classes by name in the main module, does not pollute your namespace with the hundreds of names of pygame.locals
.