<build-in function> incompatible with <function>

James Stroud jstroud at ucla.edu
Tue Jan 31 02:14:40 CET 2006


Luke wrote:
> Built-in functions don't bind to classes like regular functions.  Is
> this intended?  (I do notice that the Python Reference Manual sec 3.2
> under "Class Instance" refers to a "user-defined function").  Any ideas
> what the reason is for this distinction between build-in functions and
> normal functions?
> 
> It's rather inconvenient when implementing some methods (not the whole
> class) in a C extension :-(
> 
> $ python
> Python 2.4.2 (#1, Nov  3 2005, 12:41:57)
> [GCC 3.4.3-20050110 (Gentoo Linux 3.4.3.20050110, ssp-3.4.3.20050110-0,
> pie-8.7 on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> 
>>>>def normal_func(x):
> 
> ...   return x
> ...
> 
>>>>class foo(object):
> 
> ...   a = normal_func
> ...   b = lambda x : x
> ...   c = abs
> ...
> 
>>>>obj = foo()
>>>>obj.a
> 
> <bound method foo.normal_func of <__main__.foo object at 0xb7c3766c>>
> 
>>>>obj.b
> 
> <bound method foo.<lambda> of <__main__.foo object at 0xb7c3766c>>
> 
>>>>obj.c
> 
> <built-in function abs>
> 

py> import types
py> def doit(x):
...   print x
...
py> class bob:
...   pass
...
py> b = bob
py> b.x = types.MethodType(doit, b)
py> b.x
<bound method ?.doit of <class __main__.bob at 0x403d8b3c>>
py> b.x()
__main__.bob



More information about the Python-list mailing list