Let's consider an application where I have a base class Move and its variants Walk and Run. I would like to split them into separate classes. I would also like to be able to get a list of possible movements from the base class. Something like:
Module 1:
class Move(object): pass
Module 2:
from module1 import Move
class Walk(Move): pass
Main script:
from module1 import *
from module2 import *
print Move.__subclasses__()
Without these two import lines the main will return no subclasses. This seems to be an expected behaviour.
I would like to be able to add more movement modules in future, without modifying the existing source code.
I am thinking of discovering and importing all python files in a given directory, similar to what is done here: Python __subclasses__() not listing subclasses
Is there a cleaner way for such task?
In future, if we see any need for additional layers (Move -> Swim -> (Butterfly, Breaststroke), we were planning to set up a recursive subclasses search similar to http://stackoverflow.com/questions/3862310/how-can-i-find-all-subclasses-of-a-given-class-in-python
However now that I am rethinking of all this, looping through all modules will just to the same. – Eka AW Sep 01 '15 at 16:22