python@deprecated decorator不支持旧的python类、函数或方法。

Deprecated的Python项目详细描述


不推荐使用很容易使用

如果需要将函数或方法标记为已弃用, 您可以使用@deprecateddecorator:

保存在hello.py:

fromdeprecatedimportdeprecated@deprecated(version='1.2.1',reason="You should use another function")defsome_old_function(x,y):returnx+yclassSomeClass(object):@deprecated(version='1.3.0',reason="This method is deprecated")defsome_old_method(self,x,y):returnx+ysome_old_function(12,34)obj=SomeClass()obj.some_old_method(5,8)

而且易于设置

运行它:

$ pip install Deprecated
$ python hello.py
hello.py:15: DeprecationWarning: Call to deprecated function(or staticmethod) some_old_function.
(You should use another function) -- Deprecated since version 1.2.0.
  some_old_function(12, 34)
hello.py:17: DeprecationWarning: Call to deprecated method some_old_method.
(This method is deprecated) -- Deprecated since version 1.3.0.
  obj.some_old_method(5, 8)

您可以记录代码

您是否想过如何记录某些函数、类、方法等已弃用? 现在可以使用集成的sphinx指令执行此操作:

例如,在hello_sphinx.py:

fromdeprecated.sphinximportdeprecatedfromdeprecated.sphinximportversionaddedfromdeprecated.sphinximportversionchanged@versionadded(version='1.0',reason="This function is new")deffunction_one():'''This is the function one'''@versionchanged(version='1.0',reason="This function is modified")deffunction_two():'''This is the function two'''@deprecated(version='1.0',reason="This function will be removed soon")deffunction_three():'''This is the function three'''function_one()function_two()function_three()# warnshelp(function_one)help(function_two)help(function_three)

结果立即生效

运行:

$ python hello_sphinx.py

hello_sphinx.py:23: DeprecationWarning: Call to deprecated function(or staticmethod) function_three.
(This function will be removed soon) -- Deprecated since version 1.0.
  function_three()# warns

Help on function function_one in module __main__:

function_one()
    This is the function one

    .. versionadded:: 1.0
       This function is new

Help on function function_two in module __main__:

function_two()
    This is the function two

    .. versionchanged:: 1.0
       This function is modified

Help on function function_three in module __main__:

function_three()
    This is the function three

    .. deprecated:: 1.0
       This function will be removed soon

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
字符串Java字母替换无效   java Spring Roo JPA MS SQL Server无法打开JPA EntityManager组织。冬眠例外GenericJDBCException:无法打开连接   在scala中使用JavaWS对大型数据文件进行java流式处理   Java编译器是否将字节和短字符识别为文本?   java无法查找符号错误,空指针   mongodb在Java中重用数据库连接   java将多个StringArray从字符串文件获取到活动中   java是一个变量,它只保存最后一次鼠标单击的坐标   c#尺寸有限;添加、删除和洗牌   java如何在Android中显示来自资产文件夹的文本文件中的文本   Android应用程序中的java Tensorflow Lite自定义对象检测模型错误   java如何在foreachloop中使用scanner将来自命令行的输入存储到数组中   java如何定义一个好的存储库接口   Android中的java解析动态json对象