鱼C论坛

 找回密码
 立即注册
查看: 281|回复: 0

[技术交流] AttributeError--PythonBIF(3)

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

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

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

x
原文:

  1. Help on class AttributeError in module builtins:

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

翻译:

  1. 模块内置类中有关属性错误(AttributeError)类的帮助:

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

当访问对象时,如果该对象没有特定的属性或方法,就会出现该异常。当对对象进行无效的属性引用或赋值时,会引发AttributeError。
1.拼写错误的属性:

  1. >>>class C:
  2.             def __init__(self, name):
  3.                  self.name = name

  4. >>>c = C("小甲鱼")
  5. >>>c.age
  6. Traceback (most recent call last):
  7.   File "<pyshell#18>", line 1, in <module>
  8.     c.age
  9. AttributeError: 'C' object has no attribute 'age'  #访问了不存在的属性
复制代码

2.对象类型错误

  1. >>>myage = 18
  2. >>>myage.spilt()  #试图对整数类型进行字符串方法
  3. Traceback (most recent call last):
  4.   File "<pyshell#26>", line 1, in <module>
  5.     age.spilt()
  6. AttributeError: 'int' object has no attribute 'spilt'
复制代码

3.作用域错误

  1. >>>class C:
  2.           name = "小甲鱼"
  3.           _age = 18  #创建了一个私有变量_age
  4. >>>c = C()
  5. >>>c.age
  6. Traceback (most recent call last):
  7.   File "<pyshell#36>", line 1, in <module>
  8.     c.age
  9. AttributeError: 'C' object has no attribute 'age'
复制代码

在访问属性或方法之前,最好先检查它在对象上是否存在。你可以使用内置的hasattr()函数在访问之前检查属性是否存在。这个函数接受两个参数:对象和属性名作为字符串。如果属性存在,它返回True,否则返回False。通过在访问属性之前使用hasattr(),你可以避免AttributeError,如果属性缺失,则采取适当的操作。可以使用isinstance()函数
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-5-6 02:09

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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