Wrapping methods of built-in dict

shailesh kochhar.mw at gmail.com
Wed Jun 3 02:15:49 EDT 2009

On May 21, 10:13 pm, George Sakkis <george.sak... at gmail.com> wrote:
> 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 ?

The use case I'm exploring is automatic lock acquisition and release.
I've been trying to create a decorator which protects objects against
concurrent modification by placing all attribute behind a mutex. More
fine grained locking will probably perform better, but the convenience
and reliability of auto-locking is nice. Suggestions for alternate
implementations are most welcome.

  - Shailesh

More information about the Python-list mailing list