What would be the pythonic way to achieve the following usecase:
- Import a module conditionally
- Inherit from that module
- Call super.__init__() with parameters based on which module we inherited from
Eg. I am trying to have my app compatible with both cmd and cmd2 module. Since cmd2 supports history, I need to call its init with additional parameters. But it's ok if user doesn't have that module installed
I am handling it this way. Though it works, I don't like the solution myself
try:
import cmd2 as CMD
except:
import cmd as CMD
class MyApp(CMD.Cmd):
def __init__(self):
if globals()['CMD'].__name__ == "cmd2":
super().__init__(persistent_history_file="~/myapp_history.dat", allow_cli_args=False)
else:
super().__init__()
Note: This question is not specific to cmd/cmd2 module. That is just an example
I am trying to find the best way to achieve this for any such usecase