Wrapping methods of built-in dict

George Sakkis george.sakkis at gmail.com
Fri May 22 01:13:32 EDT 2009


On May 21, 5:55 pm, shailesh <kochhar... at gmail.com> wrote:

> There doesn't seem to be a predicate returning method wrappers. Is
> there an alternate way to query an object for attributes that are of
> method wrappers?

Sure:
>>> MethodWrapper = type({}.__init__)
>>> isinstance([].__len__, MethodWrapper)
True

But you're better off catching everything by checking with callable()
(or equivalently hasattr(obj, '__call__')).

> This exercise also makes me question if I'm going about this
> correctly. If I want to add functionality to the methods of a class or
> an object are decorators and the inspect module the pythonic way to go
> about it? I can think of alternative implementations either through
> metaclasses or proxy objects.

In my experience, it's quite unlikely to really want to decorate
indiscriminately *all* methods of a class/instance, let alone all the
special methods (e.g. __getattribute__ very rarely needs to be
overridden). Do you have an actual use case or are you just playing
around ?

George



More information about the Python-list mailing list