python 标准库中的装饰器(特别是@deprecated)
我需要将例程标记为已弃用,但显然没有用于弃用的标准库装饰器。我知道它的配方和警告模块,但我的问题是:为什么这个(常见)任务没有标准库装饰器?
附加问题:标准库中有标准装饰器吗?
I need to mark routines as deprecated, but apparently there's no standard library decorator for deprecation. I am aware of recipes for it and the warnings module, but my question is: why is there no standard library decorator for this (common) task ?
Additional question: are there standard decorators in the standard library at all ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这是一些片段,根据 Leandro 引用的内容进行修改:
因为在某些解释器中,暴露的第一个解决方案(没有过滤器处理)可能会导致警告抑制。
Here's some snippet, modified from those cited by Leandro:
Because in some interpreters the first solution exposed (without filter handling) may result in a warning suppression.
这是另一个解决方案:
这个装饰器(实际上是一个装饰器工厂)允许您给出原因消息。通过提供源文件名和行号来帮助开发人员诊断问题也更有用。
编辑:此代码使用 Zero 的建议:将
warnings.warn_explicit
行替换为warnings.warn(msg, Category=DeprecationWarning, stacklevel=2)
,它打印函数调用站点而不是函数定义站点。它使调试更加容易。
EDIT2:此版本允许开发人员指定可选的“原因”消息。
您可以将此装饰器用于函数、方法和类。
这是一个简单的示例:
您将得到:
EDIT3: 这个装饰器现在是 Deprecated 库的一部分:
新稳定版本 v1.2.13
Here is another solution:
This decorator (a decorator factory in fact) allow you to give a reason message. It is also more useful to help the developer to diagnose the problem by giving the source filename and line number.
EDIT: This code use Zero's recommendation: it replace
warnings.warn_explicit
line bywarnings.warn(msg, category=DeprecationWarning, stacklevel=2)
,which prints the function call site rather than the function definition site. It makes debugging easier.
EDIT2: This version allow the developper to specify an optional "reason" message.
You can use this decorator for functions, methods and classes.
Here is a simple example:
You'll get:
EDIT3: This decorator is now part of the Deprecated library:
New stable release v1.2.13 ????
未来的 Python 版本(3.13 之后)将包含
warnings.deprecated
装饰器 表示不推荐使用诸如mypy
之类的类型检查器。A future Python version (after 3.13) will include the
warnings.deprecated
decorator which will indicate deprecations to type checkers likemypy
.按照 muon 的建议,您可以安装
弃用
包。安装
示例用法
请参阅 http://deprecation.readthedocs.io/ 获取完整文档。
As muon suggested, you can install the
deprecation
package for this.Installation
Example Usage
See http://deprecation.readthedocs.io/ for the full documentation.
我猜原因是 Python 代码无法静态处理(就像 C++ 编译器所做的那样),在实际使用某些东西之前你无法收到有关使用某些东西的警告。我认为用一堆消息向脚本的用户发送垃圾邮件“警告:此脚本的开发人员正在使用已弃用的 API”并不是一个好主意。
更新:但是您可以创建装饰器,将原始功能转换为另一个功能。新函数将标记/检查开关,告知该函数已被调用,并且仅在将开关切换到打开状态时才显示消息。和/或在退出时,它可能会打印程序中使用的所有已弃用函数的列表。
I guess the reason is that Python code can't be processed statically (as it done for C++ compilers), you can't get warning about using some things before actually using it. I don't think that it's a good idea to spam user of your script with a bunch of messages "Warning: this developer of this script is using deprecated API".
Update: but you can create decorator which will transform original function into another. New function will mark/check switch telling that this function was called already and will show message only on turning switch into on state. And/or at exit it may print list of all deprecated functions used in program.
您可以创建一个 utils 文件
,然后导入弃用装饰器,如下所示:
You can create a utils file
And then import the deprecation decorator as follows:
Python
是一种动态类型语言。不必静态地将函数的类型声明为变量或参数类型。因为它是动态的,如果在运行时处理的话,一切都是动态的。即使某个方法已被弃用,它也只能在运行时或解释期间知道。
使用 deprecation 模块来弃用方法。
安装:
小演示:
输出:
Python
is a dynamically typed language. Not necessary declare the type to variable or argument type for function statically.Since its dynamic every thing if processed at runtime. Even if a method is deprecated it will be known at runtime or during interpretation only.
use deprecation module to deprecate methods.
Installing :
Small demonstration:
Output: