Method binding confusion

Andrew Bennetts andrew-pythonlist at puzzling.org
Mon May 3 20:01:14 EDT 2004


On Mon, May 03, 2004 at 07:52:02PM +0200, Peter Otten wrote:
[...]
> 
> The machinery behind the observed behaviour is now somewhat clearer - I
> think you can predict a function's behaviour as a method by checking
> hasattr(func, "__get__"). But still the *reason* (or simply use-case) for
> this dichotomy of functions (to me) remains unclear. Why are not all
> functions created equal?

I suppose that BuiltinFunctionType is probably slightly simpler and faster
this way.  After all, how often is an instance of a user-defined class going
to be right type to be the first argument to a builtin function?  (I can
think of contrived cases, but I've never come across a need for this
behaviour, which is why I'd never noticed it was missing until this thread.)

That's just me speculating, though...

Interestingly,
>>> import types
>>> types.BuiltinFunctionType is types.BuiltinMethodType
True

(types.BuiltinFunctionType seems to distinguish between functions and
methods by looking at its __self__ attribute; builtin functions have a
__self__ of None).

-Andrew.





More information about the Python-list mailing list