Turning builtin functions into methods.

Jacek Generowicz jacek.generowicz at cern.ch
Tue Dec 9 05:17:59 EST 2003


Jacek Generowicz <jacek.generowicz at cern.ch> writes:

> Functions defined in Python have type types.FunctionType, and are
> descriptors whose __get__ method turns them into bound or unbound
> methods. Functions defined in extension modules have type
> types.BuiltinFunctionType, and have no __get__ method. Adding them as
> attributes to classes and calling them through an instance of the
> class does not result in them being called as methods: self is lost.
> 
> What's the simplest way of getting around this ?

By trial and error, I seem to have found that passing None as the
second argument (the instance) to new.instancemethod, does the trick.

The library reference manual confirms that it is actually supposed to
work this way. It's a pity that the builtin documentation does not:

  Help on built-in function instancemethod:

  instancemethod(...)
      Create a instance method object from (FUNCTION, INSTANCE, CLASS).
  (END) 




More information about the Python-list mailing list