python - AttributeError 的起源:对象没有属性“cos”
问题描述
我遇到了这篇文章:Sympy to numpy 导致 AttributeError: 'Symbol' object has no attribute 'cos',并且想更多地了解异常的起源。
更准确地说,Eric 回答说:“当您调用 np.cos(a_symbol) 时会发生这种类型的错误,这显然会将 numpy 的底层转换为 a_symbol.cos()。” 我想了解这种行为的来源/来源: np.cos(x) 如何在幕后翻译成 x.cos() ?
我试图重现异常的引发,但无法追踪它的起源:
import numpy as np
class toto:
def __init__(self,x):
self.x = x
foo = toto(4)
class tata:
def __init__(self,x):
self.x = x
def cos(self):
print(self.x)
bar = tata(5)
try:
np.cos(foo)
except:
np.cos(bar)
这将打印 5。
干杯
解决方案
推荐阅读
- javascript - 使用 Ajax 在选择更改时更改 PHP 变量并在 SQL 查询中使用
- java - 仅更改某些组件的字体
- css - 动画完成后Animate.css动画消失
- javascript - 如何将 props 从容器组件传递到类组件
- sql - 当表包含 900 万条记录时,H2 数据库无法使用
- python - 如何使用 for 循环中的一组数据从外推(线性回归?)中获得下一个预测值?
- python - 如何正确使用reduce和字典
- python - 是否有一个 python 工具可以生成类似覆盖的报告,但对于异常,函数会引发?
- kubernetes - 将 skaffold 配置文件绑定到集群
- python - 为什么 django 模板没有显示任何输出?