Say I have a package “colorExtentions” and in my __init__.py I have __all__=["red", "blue"]. This package should be able to be extended automatically by adding modules to the directory and to the __all__ list. Is the following acceptable or is there another “best practice” for such kind of things?
import colorExtentions
from colorExtentions import *
for moduleName in colorExtentions.__all__:
colors.append(moduleName)
promptUserToChoose(colors)
Presuming that each colour is a class, I would argue the nicer solution would be to ensure all of your colours subclass from a single class (which is good design anyway):
Then, when you want a list of all colours, you can simply import any modules that will contain the classes, and do:
This way you end up with a list of each
Colorsubclass.Note in 2.x, you will need new-style classes (inheriting from
object) to have the__subclasses__()method available.