鱼C论坛

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

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

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

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

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

x
原文:
Help on class AttributeError in module builtins:

class AttributeError(Exception)
 |  Attribute not found.
 |
 |  Method resolution order:
 |      AttributeError
 |      Exception
 |      BaseException
 |      object
 |
 |  Methods defined here:
 |
 |  __getstate__(self, /)
 |      Helper for pickle.
 |
 |  __init__(self, /, *args, **kwargs)
 |      Initialize self.  See help(type(self)) for accurate signature.
 |
 |  __reduce__(self, /)
 |      Helper for pickle.
 |
 |  __str__(self, /)
 |      Return str(self).
 |
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |
 |  name
 |      attribute name
 |
 |  obj
 |      object
 |
 |  ----------------------------------------------------------------------
 |  Static methods inherited from Exception:
 |
 |  __new__(*args, **kwargs) class method of builtins.Exception
 |      Create and return a new object.  See help(type) for accurate signature.
 |
 |  ----------------------------------------------------------------------
 |  Methods inherited from BaseException:
 |
 |  __getattribute__(self, name, /)
 |      Return getattr(self, name).
 |
 |  __repr__(self, /)
 |      Return repr(self).
 |
 |  __setstate__(self, object, /)
 |
 |  add_note(self, object, /)
 |      Exception.add_note(note) --
 |      add a note to the exception
 |
 |  with_traceback(self, object, /)
 |      Exception.with_traceback(tb) --
 |      set self.__traceback__ to tb and return self.
 |
 |  ----------------------------------------------------------------------
 |  Data descriptors inherited from BaseException:
 |
 |  __cause__
 |      exception cause
 |
 |  __context__
 |      exception context
 |
 |  __dict__
 |
 |  __suppress_context__
 |
 |  __traceback__
 |
 |  args
翻译:
模块内置类中有关属性错误(AttributeError)类的帮助:

类属性错误(异常)
 | 未找到属性。
 |
 | 方法解析顺序:
 | 属性错误
 | 异常
 | 基本异常
 | 对象
 |
 | 这里定义的方法:
 |
 | __getstate__(self, /)
 | pickle 的助手。
 |
 | __init__(self, /, *args, **kwargs)
 | 初始化自身。 有关准确的签名,请参见 help(type(self)) 。
 |
 | __reduce__(self, /)
 | pickle 的助手。
 |
 | __str__(self, /)
 | 返回 str(self)。
 |
 | ----------------------------------------------------------------------
 | 此处定义的数据描述符:
 |
 | 名称
 | 属性名称
 |
 | 对象
 | 对象
 |
 | ----------------------------------------------------------------------
 | 继承自 Exception 的静态方法:
 |
 | 内置函数 Exception 的 __new__(*args, **kwargs) 类方法
 | 创建并返回一个新对象。 请参见 help(type) 获取准确的签名。
 |
 | ----------------------------------------------------------------------
 | 继承自 BaseException 的方法:
 |
 | __getattribute__(self, name, /)
 | 返回 getattr(self,name)。
 |
 | __repr__(self, /)
 | 返回 repr(self)。
 |
 | __setstate__(self, object, /)
 |
 | add_note(self, object, /)
 | Exception.add_note(note) --
 | 为异常添加注释
 |
 | with_traceback(self, object, /)
 | Exception.with_traceback(tb) -- | 为异常添加注释。
 | 将 self.__traceback__ 设为 tb 并返回 self。
 |
 | ----------------------------------------------------------------------
 | 继承自 BaseException 的数据描述符:
 |
 | __cause__
 | 异常原因
 |
 | __context__
 | 异常上下文
 |
 | __dict__
 |
 | __suppress_context__
 |
 | __traceback__
 |
 | args
当访问对象时,如果该对象没有特定的属性或方法,就会出现该异常。当对对象进行无效的属性引用或赋值时,会引发AttributeError。
1.拼写错误的属性:
>>>class C:
            def __init__(self, name):
                 self.name = name

>>>c = C("小甲鱼")
>>>c.age
Traceback (most recent call last):
  File "<pyshell#18>", line 1, in <module>
    c.age
AttributeError: 'C' object has no attribute 'age'  #访问了不存在的属性
2.对象类型错误
>>>myage = 18
>>>myage.spilt()  #试图对整数类型进行字符串方法
Traceback (most recent call last):
  File "<pyshell#26>", line 1, in <module>
    age.spilt()
AttributeError: 'int' object has no attribute 'spilt'
3.作用域错误
>>>class C:
          name = "小甲鱼"
          _age = 18  #创建了一个私有变量_age
>>>c = C()
>>>c.age
Traceback (most recent call last):
  File "<pyshell#36>", line 1, in <module>
    c.age
AttributeError: 'C' object has no attribute 'age'
在访问属性或方法之前,最好先检查它在对象上是否存在。你可以使用内置的hasattr()函数在访问之前检查属性是否存在。这个函数接受两个参数:对象和属性名作为字符串。如果属性存在,它返回True,否则返回False。通过在访问属性之前使用hasattr(),你可以避免AttributeError,如果属性缺失,则采取适当的操作。可以使用isinstance()函数
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-2-24 02:41

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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