Show warning when a class is imported in python

前端 未结 4 932
臣服心动
臣服心动 2021-01-13 11:52

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?

module 1

4条回答
  •  失恋的感觉
    2021-01-13 12:16

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

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

提交回复
热议问题