__getattr__ equivalent for methods

When the attribute is not found, object.__getattr__ . Is there an equivalent way to intercept undefined methods?

+4
source share
3 answers

Ways are also attributes. __getattr__ works the same for them:

 class A(object): def __getattr__(self, attr): print attr 

Then try:

 >>> a = A() >>> a.thing thing >>> a.thing() thing Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'NoneType' object is not callable 
+8
source

There is no difference. The method is also an attribute. (If you want the method to have an implicit β€œi” argument, you will have to do one more job to bind the method).

+9
source

you have not returned anything.

 class A(object): def __getattr__(self, attr): return attr 

must work

0
source

All Articles