Python 当类从a.py更改为b.py时,不推荐从a.py导入
我在a.py中有一个类“Model”,现在该类已移动到b.py。因此,每当最终用户从导入模型中使用时,我都希望通过一个不推荐或一些警告消息来验证。你知道怎么做吗?你应该使用warning()和更特别的DeprecationWarning()Python 当类从a.py更改为b.py时,不推荐从a.py导入,python,deprecated,deprecation-warning,Python,Deprecated,Deprecation Warning,我在a.py中有一个类“Model”,现在该类已移动到b.py。因此,每当最终用户从导入模型中使用时,我都希望通过一个不推荐或一些警告消息来验证。你知道怎么做吗?你应该使用warning()和更特别的DeprecationWarning() 这里已经有一个很好的答案:我提出了一个简单的解决方案: class Model: def __init__(self): # prints a warning message in the console print(
这里已经有一个很好的答案:我提出了一个简单的解决方案:
class Model:
def __init__(self):
# prints a warning message in the console
print('The "Model" class has been moved to b.py, please consider modifying your code !')
m = Model() # instantiate the model class to throw the message
这应该像你期望的那样工作
编辑:这是a.py的代码谢谢你的回答。如果我移动了一组类怎么办?我需要创建所有方法吗?