pythonmetaprogramming

How to check if object attribute is of type method-wrapper?


Some of the callable() attributes of an object are of type <class 'method-wrapper'>. Is it possible to check if a callable is of type method-wrapper?

For example isinstance(object, methodwrapper) or something like that?


Basically what I am trying to achieve is this: I have an object a of type A and I want to serialize it so I can send it over the wire. I also want to "serialize" the methods of that object. In particular, I want to send the source for each of the methods over the wire so it can be executed in some scope on the other side and injected into a re-creation of the a because the methods may have been changed or updated etc. So I go over all the methods, "serializing" them one at a time and then sending the json dictionary of attribute_name -> attribute over the wire. But I don't want to try to serialize methods of type method-wrapper. Now one way would just be to try and catch an exception when trying to serialize method-wrappers but I was wondering if there's another way.


Solution

  • At the very least, you could do str(type(a.foo)) == '<class 'method-wrapper'> or something similar. If you have a guaranteed way to find a method that prints like that, you should also be able to do methodwrapper = a.foo.__class__ which would then allow you to be able to do isinstance(foo, methodwrapper). However, it's likely that there's a module which exposes these directly, but it's not in builtins that I can see. Not sure where else to look.

    Edit:

    >>> type(all.__call__)
    <type 'method-wrapper'>