Reputation: 11773
I have a question related to Python file management. The way how I organize my Python files are as follows (__init__.py
is empty):
--src
|---__init__.py
|---module1
| |----- __init__.py
| |----- my_file1.py
|---module2
| |----- __init__.py
| |----- my_file2.py
--app
|---my_application.py
As you can see, I separate my codes into two groups: the first group of codes, which is located in src
directory, contains self-defined library codes; the second group of codes, which is located in app
directory, contains the application codes that will call the self-defined library. my_application.py
may contain the following codes:
from src.module1.my_file1 import ClassA1, ClassA2, ClassA3
from src.module2.my_file2 import ClassB1, ClassB2, ClassB3
a = ClassA1()
b = ClassB3()
It is boring to import classes from self-defined library whenever I write an application based on it. I would like to have something like that:
Request 1:
import all classes defined in src.module1
Request 2:
import fundamental classes defined in src.module1 and src.module2
Here fundamental classes may refer to ClassA1
in src.module1.my_file1
and ClassB2
in src.module2.my_file2
In C++, this can be realized very easily. For example, for the second requirement, I can put all the header files that are related to fundamental classes in one head file. Then in the application program, I just include this head file. But I have no ideas how Python can work. Thanks.
Upvotes: 2
Views: 86
Reputation: 11773
The answer is based on @Xatyrian's comments buy using __all__
keyword in Python (a discussion on Stackoverflow can be found here, @Aaron Hall's comments are very good)
Request 1: import all classes defined in src.module1
Please check @Marshal Hayes answer in this discussion. Basically, we are going to import the module using import module_name
Request 2: import fundamental classes defined in src.module1 and src.module2
This can be done using the keyword __all__
.
--src
|---__init__.py (1)
|---module1
| |----- __init__.py (2)
| |----- my_file1.py
|---module2
| |----- __init__.py (3)
| |----- my_file2.py
--app
|---my_application.py
In (1) __init__.py
we can denote which module we want to export ( we export the module if we want to export functions from this module). In (2) and (3) we denote which file we want to export (we export the file if we want to export functions from this file). Then in each function implementation file, for example, my_file1.py we can list all functions we want to export in __all__=[fun1,fun2,fun3]
keyword.
Then when we want to use the exported functions in application file, we simply import the top module name. In our example, we will do the following:
import src
src.fun1()
src.fun2()
print(dir(src)) : list all functions and modules in this package
Upvotes: 0
Reputation: 73
To answer your first question, you can simply replace from src.module1.my_file1 import ClassA1, ClassA2, ClassA3
with import src.module.my_file1
This will let you access all classes in the module. If you want, you can also name the imported module like import module as md
which will let you access all classes in the module like a = md.ClassA1()
As for your second question, I don't think there will ever be a need to only import a few classes from a module. The classes won't be instantiated unless called, so no memory will be allocated for them.
Upvotes: 1