Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 当类从a.py更改为b.py时,不推荐从a.py导入_Python_Deprecated_Deprecation Warning - Fatal编程技术网

Python 当类从a.py更改为b.py时,不推荐从a.py导入

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(

我在a.py中有一个类“Model”,现在该类已移动到b.py。因此,每当最终用户从导入模型中使用时,我都希望通过一个不推荐或一些警告消息来验证。你知道怎么做吗?

你应该使用warning()和更特别的DeprecationWarning()


这里已经有一个很好的答案:

我提出了一个简单的解决方案:

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的代码

谢谢你的回答。如果我移动了一组类怎么办?我需要创建所有方法吗?