<build-in function> incompatible with <function>

L

Luke

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..... return x
........ a = normal_func
.... b = lambda x : x
.... c = abs
....<built-in function abs>
 
G

gmane

Luke said:
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?

no, but does this help...
.... c = classmethod(abs)
....

Emile
 
J

James Stroud

Luke said:
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.

... return x
...

... a = normal_func
... b = lambda x : x
... c = abs
...

<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
 
L

Luke

Thanks James, though from the output of b.x() it appears that x is a
class method (ie the class is passed as the first parameter rather than
the instance)...
 
J

James Stroud

Luke said:
Thanks James, though from the output of b.x() it appears that x is a
class method (ie the class is passed as the first parameter rather than
the instance)...

Sorry, the one line was probably supposed to be

b = bob()

I forgot the parens:

py> b = bob()
py> b.x = types.MethodType(doit, b)
py> b.x()
<__main__.bob instance at 0x404afb6c>
py> b.x
<bound method ?.doit of <__main__.bob instance at 0x404afb6c>>

James
 
J

James Stroud

James said:
Sorry, the one line was probably supposed to be

b = bob()

I forgot the parens:

py> b = bob()
py> b.x = types.MethodType(doit, b)
py> b.x()
<__main__.bob instance at 0x404afb6c>
py> b.x
<bound method ?.doit of <__main__.bob instance at 0x404afb6c>>

James

Also, you should know about the __abs__ method (this is probably what
you are really looking for:

py> class bob:
.... def __init__(self, aval):
.... self.value = aval
.... def __abs__(self):
.... return abs(self.value)
....
py> b = bob(-4)
py>
py> abs(b)
4
py> b.value
-4


You may want to have a look at this:

http://docs.python.org/ref/customization.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top