实现以下用例的pythonic方法是什么:
- 有条件地导入模块
- 从该模块继承
- 呼叫super__init__()带有基于我们从哪个模块继承的参数
我正在努力使我的应用程序与cmd
和cmd2
模块兼容.由于cmd2
支持历史,我需要使用其他参数调用其init.但如果用户没有安装该模块,也没关系
我是这样处理的.虽然有效,但我自己不喜欢这个解决方案
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 100 module. That is just an example
我正在努力找到实现这一点的最佳方法