鱼C论坛

 找回密码
 立即注册
查看: 1108|回复: 1

[已解决]为什么这里报错的内容不一样

[复制链接]
发表于 2024-8-21 11:23:49 | 显示全部楼层 |阅读模式

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

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

x
B站看到了第69讲,我的问题是:为什么代码一样,带上我的报错和小甲鱼视频里的报错不一致呢,为什么我的字符串多显示了两次,是因为版本问题吗?我的版本是3.12.4。提前谢谢大家!

我的代码和报错:
  1. class C:
  2.     def __init__(self, name, age):
  3.         self.name = name
  4.         self.__age = age
  5.     def __getattribute__(self, attrname):
  6.         print("Give it to me!")
  7.         return super().__getattribute__(attrname)
  8.     def __getattr__(self, attrname):
  9.         if attrname == "FishC":
  10.             print("I ;ove FishC!")
  11.         else:
  12.             raise AttributeError(attrname)

  13.         
  14. c = C("Little turtle", 18)
  15. c.FishC
  16. Give it to me!
  17. I ;ove FishC!
  18. c.x
  19. Give it to me!
  20. Traceback (most recent call last):
  21.   File "<pyshell#40>", line 1, in <module>
  22.     c.x
  23.   File "<pyshell#37>", line 12, in __getattr__
  24.     raise AttributeError(attrname)
  25. Give it to me!
  26. Give it to me!
  27. AttributeError: x
复制代码
小甲鱼的:
1.png


最佳答案
2024-8-21 11:33:52
哈哈,鱼油您好,听说您在写代码时遇到了些小情况,来来来,我这块椅子给您备好了,咱们慢慢聊。

### 报错内容不一致?

您提到在看小甲鱼的视频时,报错和您自己运行时不一样,这种情况其实挺正常的,尤其是不同的 Python 版本之间。好吧,接下来我们鱼鱼兄弟一起来拆解一下您的代码和它的报错机制。

### 代码怎么看?

首先,来看您的代码和报错:

  1. class C:
  2.     def __init__(self, name, age):
  3.         self.name = name
  4.         self.__age = age

  5.     def __getattribute__(self, attrname):
  6.         print("Give it to me!")
  7.         return super().__getattribute__(attrname)

  8.     def __getattr__(self, attrname):
  9.         if (attrname == "FishC"):
  10.             print("I ;ove FishC!")
  11.         else:
  12.             raise AttributeError(attrname)

  13. c = C("Little turtle", 18)
  14. c.FishC
  15. c.x
复制代码


### 报错分析

1. **`c.FishC`**
    - 您在访问 `c.FishC` 时,Python 首先调用 `__getattribute__`,所以会打印 “Give it to me!”。
    - 由于 `FishC` 并不在实例的属性中,接下来调用 `__getattr__`,打印 “I love FishC!”。

2. **`c.x`**
    - 再次,Python 调用 `__getattribute__`,输出 “Give it to me!”。
    - 因为 `x` 也不是实例的属性,于是 `__getattr__` 被调用。
    - `__getattr__` 尝试处理没有成功,因此抛出 `AttributeError(attrname)`。

### 为什么字符串多显示了两次?

我们来重点看看这段代码:

  1. return super().__getattribute__(attrname)
复制代码


由于在 Python 3.12.4 中,`super().__getattribute__(attrname)` 其实会再次触发 `__getattribute__` 方法,导致这个方法反复调用。于是,您看到的 “Give it to me!” 被打印了多次,也就是您说的多显示。

### 小甲鱼的版本?

小甲鱼的视频中可能使用了不同的 Python 版本,这种情况下一些底层实现可能有所不同。不同版本的 Python 对特定方法的调用细节可能会有所变化,但大体逻辑应该是一致的。

### 避免多次打印

为了解决这个问题,可以在 `__getattribute__` 方法内做一些小改动,比如先禁用 `__getattribute__` 方法的重入:

  1. class C:
  2.     def __init__(self, name, age):
  3.         self.name = name
  4.         self.__age = age

  5.     def __getattribute__(self, attrname):
  6.         if attrname == '__getattribute__':
  7.             return super().__getattribute__(attrname)
  8.         print("Give it to me!")
  9.         return super().__getattribute__(attrname)

  10.     def __getattr__(self, attrname):
  11.         if attrname == "FishC":
  12.             print("I love FishC!")
  13.         else:
  14.             raise AttributeError(attrname)
复制代码


这样就可以避免多次调用的情况了。

不知这一番"鱼友交流"有没有为您解惑呢?继续撸代码~ 有问题随时喊我哦!FishC 友谊的小船不会轻易翻的!

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

使用道具 举报

发表于 2024-8-21 11:33:52 | 显示全部楼层    本楼为最佳答案   
哈哈,鱼油您好,听说您在写代码时遇到了些小情况,来来来,我这块椅子给您备好了,咱们慢慢聊。

### 报错内容不一致?

您提到在看小甲鱼的视频时,报错和您自己运行时不一样,这种情况其实挺正常的,尤其是不同的 Python 版本之间。好吧,接下来我们鱼鱼兄弟一起来拆解一下您的代码和它的报错机制。

### 代码怎么看?

首先,来看您的代码和报错:

  1. class C:
  2.     def __init__(self, name, age):
  3.         self.name = name
  4.         self.__age = age

  5.     def __getattribute__(self, attrname):
  6.         print("Give it to me!")
  7.         return super().__getattribute__(attrname)

  8.     def __getattr__(self, attrname):
  9.         if (attrname == "FishC"):
  10.             print("I ;ove FishC!")
  11.         else:
  12.             raise AttributeError(attrname)

  13. c = C("Little turtle", 18)
  14. c.FishC
  15. c.x
复制代码


### 报错分析

1. **`c.FishC`**
    - 您在访问 `c.FishC` 时,Python 首先调用 `__getattribute__`,所以会打印 “Give it to me!”。
    - 由于 `FishC` 并不在实例的属性中,接下来调用 `__getattr__`,打印 “I love FishC!”。

2. **`c.x`**
    - 再次,Python 调用 `__getattribute__`,输出 “Give it to me!”。
    - 因为 `x` 也不是实例的属性,于是 `__getattr__` 被调用。
    - `__getattr__` 尝试处理没有成功,因此抛出 `AttributeError(attrname)`。

### 为什么字符串多显示了两次?

我们来重点看看这段代码:

  1. return super().__getattribute__(attrname)
复制代码


由于在 Python 3.12.4 中,`super().__getattribute__(attrname)` 其实会再次触发 `__getattribute__` 方法,导致这个方法反复调用。于是,您看到的 “Give it to me!” 被打印了多次,也就是您说的多显示。

### 小甲鱼的版本?

小甲鱼的视频中可能使用了不同的 Python 版本,这种情况下一些底层实现可能有所不同。不同版本的 Python 对特定方法的调用细节可能会有所变化,但大体逻辑应该是一致的。

### 避免多次打印

为了解决这个问题,可以在 `__getattribute__` 方法内做一些小改动,比如先禁用 `__getattribute__` 方法的重入:

  1. class C:
  2.     def __init__(self, name, age):
  3.         self.name = name
  4.         self.__age = age

  5.     def __getattribute__(self, attrname):
  6.         if attrname == '__getattribute__':
  7.             return super().__getattribute__(attrname)
  8.         print("Give it to me!")
  9.         return super().__getattribute__(attrname)

  10.     def __getattr__(self, attrname):
  11.         if attrname == "FishC":
  12.             print("I love FishC!")
  13.         else:
  14.             raise AttributeError(attrname)
复制代码


这样就可以避免多次调用的情况了。

不知这一番"鱼友交流"有没有为您解惑呢?继续撸代码~ 有问题随时喊我哦!FishC 友谊的小船不会轻易翻的!

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-8 05:16

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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