Origin of AttributeError: object has no attribute 'cos'

社会主义新天地 提交于 2019-12-13 03:06:25

问题


I came upon this post : Sympy to numpy causes the AttributeError: 'Symbol' object has no attribute 'cos', and would like to understand a bit more the origin of the exception.

To be more precise, Eric answered : "This type of error occurs when you call np.cos(a_symbol), which apparently translates under-the-hood in numpy to a_symbol.cos()." I would like to understand how/where this behaviour origins from : how does np.cos(x) can be translated under the hood into x.cos() ?

I tried to reproduce the raise of the exception, but couldn't trace it's origin:

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)

This prints 5.

Cheers

来源:https://stackoverflow.com/questions/54538582/origin-of-attributeerror-object-has-no-attribute-cos

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!