在python中导入类时显示警告 [英] Show warning when a class is imported in python

查看:185
本文介绍了在python中导入类时显示警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在其他模块中有一个类,并且要在导入该类时显示一个DeprecationWarning.正确的方法是什么?

I have a class in a different module and I want to show a DeprecationWarning when that class is imported. What will be the right way to do so?

模块1包含-

class Test:
    pass

模块2包含-

from module1 import Test #this line should show a DeprecationWarning.

推荐答案

导入将执行类定义,因此将警告放在此处:

The import will execute the class definition, so put the warning there:

class Test:
    raise DeprecationWarning('This class is deprecated')

这篇关于在python中导入类时显示警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆