Control module imports

Quickly reference of how to use to import ...

latrova commited 2 years, 9 months ago
Control - Read as you want

Let's imagine following structure

|_ A
|  |_
|  |_
|_ B


This way, if you need to use both classes in , you will need to import like this:

from A.ClassA import ClassA
from B.ClassB import ClassB

Importing name

Which can get redundant, so in A we will do the following:

from .ClassA import ClassA
__all__ = ["ClassA"]

Now we can access this way:

from A import ClassA
Simple and direct to the point, you can repeat same for ClassB in its respective

Customizing imported name

Note that __all__ should always be a list of strings with names that you've imported. If I would want to change imported name, I could:


from .ClassA import ClassA as CustomA
__all__ = ["CustomA"]

from A import CustomA
blog comments powered by Disqus