What is Python's equivalent of Ruby's method_missing method? I tried using __getattr__ but this hook applies to fields too. I only want to intercept the method invocations. What is the Python way to do it?

For reference, the link in question gets around the ambiguity by defining a list of attribute names that should be considered methods (which sounds like it kinda defeats the purpose, since you could just define each of those methods and delegate to a stub).
–
Mu MindOct 7 '12 at 16:23

Python doesn't distinguish between methods and attributes (a.k.a. "instance variables") the way Ruby does. Methods and other object attributes are looked up in exactly the same way in Python -- not even Python knows the difference at the look-up stage. Until the attribute is found, it's just a string.

So if you're asking for a way to ensure that __getattr__ is only called for methods, I'm afraid you probably won't find an elegant solution. But it's easy enough to simply return a function (or even a brand-new dynamically bound method) from __getattr__.

For the same reason you would use method_missing in Ruby or doesNotUnderstand in Smalltalk.
–
missingfaktorJul 15 '11 at 8:37

I understand why you would want to use __getattr__. I just don't understand why you "only want to intercept the method invocations".
–
senderleJul 15 '11 at 8:44

1

Ruby does not distinguish between methods and attributes at all - there is no such thing as an attribute in Ruby.
–
steenslagJul 15 '11 at 17:00

1

@steenslag, that sounds to me like a really bizarre claim. When I say "attribute" I mean "internal state". Are you claiming that objects in Ruby have no internal state? Or do you mean that the internal state of objects in Ruby is always private? That's true. I suppose in Ruby-speak an attribute is really an accessor method of an "instance variable". But since we're talking about Python, I'm using Python-speak.
–
senderleJul 15 '11 at 17:19

1

In Ruby terms, Python's __getattr__ is somewhere between method_missing and overriding Hash#[] to do something special for missing keys.
–
Mark ReedAug 31 '13 at 20:18

,this sort of comes closer to implementing the behaviour of calling the special method for every name that does not correspond to a callable attribute/method. Of course they still don't really have separate namespaces so it may feel a bit weird. It works by overidding __getattribute__ which works at a lower level then __getattr__ it tries to fetch an attribute if it fails it returns a curried special method to call with the name you called it with, if it succeeds it passes it on if its callable otherwise it
wraps the result with a proxy object which acts in almost exactly the same way afterwards except it implements call with your special method.

It doesn't allow you to access the calling object because I couldn't think of a good way to do that without sort of leaking memory(the calling object) if it's already a non-callable attribute which you store(the only think I can think of is to start a new thread that deletes it after a minute, by then you have presumably called it unless you are using it in a closure which wouldn't be supported in that case).