鱼C论坛

 找回密码
 立即注册
查看: 289|回复: 5

[技术交流] DeprecationWarning--PythonBIF(15)

[复制链接]
发表于 2025-1-27 20:51:15 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
原文:

  1. Help on class DeprecationWarning in module builtins:

  2. class DeprecationWarning(Warning)
  3. |  Base class for warnings about deprecated features.
  4. |
  5. |  Method resolution order:
  6. |      DeprecationWarning
  7. |      Warning
  8. |      Exception
  9. |      BaseException
  10. |      object
  11. |
  12. |  Methods defined here:
  13. |
  14. |  __init__(self, /, *args, **kwargs)
  15. |      Initialize self.  See help(type(self)) for accurate signature.
  16. |
  17. |  ----------------------------------------------------------------------
  18. |  Static methods defined here:
  19. |
  20. |  __new__(*args, **kwargs)
  21. |      Create and return a new object.  See help(type) for accurate signature.
  22. |
  23. |  ----------------------------------------------------------------------
  24. |  Methods inherited from BaseException:
  25. |
  26. |  __getattribute__(self, name, /)
  27. |      Return getattr(self, name).
  28. |
  29. |  __reduce__(self, /)
  30. |      Helper for pickle.
  31. |
  32. |  __repr__(self, /)
  33. |      Return repr(self).
  34. |
  35. |  __setstate__(self, object, /)
  36. |
  37. |  __str__(self, /)
  38. |      Return str(self).
  39. |
  40. |  add_note(self, object, /)
  41. |      Exception.add_note(note) --
  42. |      add a note to the exception
  43. |
  44. |  with_traceback(self, object, /)
  45. |      Exception.with_traceback(tb) --
  46. |      set self.__traceback__ to tb and return self.
  47. |
  48. |  ----------------------------------------------------------------------
  49. |  Data descriptors inherited from BaseException:
  50. |
  51. |  __cause__
  52. |      exception cause
  53. |
  54. |  __context__
  55. |      exception context
  56. |
  57. |  __dict__
  58. |
  59. |  __suppress_context__
  60. |
  61. |  __traceback__
  62. |
  63. |  args
复制代码

翻译:

  1. 模块内置类中有关类 DeprecationWarning 的帮助:

  2. 类 DeprecationWarning(Warning)
  3. | 用于警告已过时功能的基类。
  4. |
  5. | 方法解析顺序:
  6. | 方法解决顺序
  7. | 警告
  8. | 异常
  9. | 基本异常
  10. | 对象
  11. |
  12. | 这里定义的方法:
  13. |
  14. | __init__(self, /, *args, **kwargs)
  15. | 初始化自身。 有关准确的签名,请参见 help(type(self)) 。
  16. |
  17. | ----------------------------------------------------------------------
  18. | 此处定义的静态方法:
  19. |
  20. | __new__(*args, **kwargs)
  21. | 创建并返回一个新对象。 有关准确的签名,请参见 help(type)。
  22. |
  23. | ----------------------------------------------------------------------
  24. | 继承自 BaseException 的方法:
  25. |
  26. | __getattribute__(self, name, /)
  27. | 返回 getattr(self,name)。
  28. |
  29. | __reduce__(self, /)
  30. | pickle 的辅助函数。
  31. |
  32. | 返回 __repr__(self, /)
  33. | 返回 repr(self)。
  34. |
  35. | __setstate__(self, object, /)
  36. |
  37. |__str__(self, /)
  38. | 返回 str(self)。
  39. |
  40. | add_note(self, object, /)
  41. | Exception.add_note(note) --
  42. | 为异常添加注释
  43. |
  44. | with_traceback(self, object, /)
  45. | Exception.with_traceback(tb) -- | 为异常添加注释。
  46. | 将 self.__traceback__ 设为 tb 并返回 self。
  47. |
  48. | ----------------------------------------------------------------------
  49. | 从 BaseException 继承的数据描述符:
  50. |
  51. | __cause__
  52. | 异常原因
  53. |
  54. | __context__
  55. | 异常上下文
  56. |
  57. | __dict__
  58. |
  59. | __suppress_context__
  60. |
  61. | __traceback__
  62. |
  63. | args
复制代码


当收到这个警告,说明这个功能将在未来的python版本中移除(个人一次也没遇到过)
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2025-1-27 20:51:24 | 显示全部楼层
当收到这个警告时,说明这个功能将在未来的Python版本中移除。下面是一个关于如何处理 DeprecationWarning 的示例:

示例代码:忽略 DeprecationWarning

  1. import warnings

  2. # 忽略 DeprecationWarning
  3. warnings.filterwarnings("ignore", category=DeprecationWarning)

  4. # 触发 DeprecationWarning 的代码
  5. def old_function():
  6.     warnings.warn("这个功能即将被移除", DeprecationWarning)

  7. old_function()
复制代码


示例代码:捕获 DeprecationWarning

  1. import warnings

  2. # 捕获 DeprecationWarning
  3. with warnings.catch_warnings(record=True) as w:
  4.     warnings.simplefilter("always")
  5.    
  6.     # 触发 DeprecationWarning 的代码
  7.     def old_function():
  8.         warnings.warn("这个功能即将被移除", DeprecationWarning)
  9.    
  10.     old_function()
  11.    
  12.     # 检查警告是否被触发
  13.     if w:
  14.         print(f"捕获到警告: {w[-1].message}")
复制代码


希望这些示例能帮助你更好地理解和处理 DeprecationWarning 警告!

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2025-1-27 21:02:30 | 显示全部楼层
可以考虑整合一下,将多个异常发在一篇帖子里
我提供python官方文档的连接
https://docs.python.org/zh-cn/3/library/exceptions.html
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2025-1-28 08:38:46 From FishC Mobile | 显示全部楼层
smallwh 发表于 2025-1-27 03:02
可以考虑整合一下,将多个异常发在一篇帖子里
我提供python官方文档的连接
https://docs.python.org/zh-c ...

是的,我也觉得,要不太多了。今天整整
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 6 天前 | 显示全部楼层
水贴是论坛禁止的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 5 天前 From FishC Mobile | 显示全部楼层
player-none 发表于 2025-4-29 04:09
水贴是论坛禁止的

哈哈,确实。不整了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-5 16:01

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表