Origin of AttributeError: object has no attribute

2019-08-24 04:37发布

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

0条回答
登录 后发表回答