<build-in function> incompatible with <function>

James Stroud jstroud at ucla.edu
Mon Jan 30 21:51:12 EST 2006


James Stroud wrote:
> Luke wrote:
> 
>> 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

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



More information about the Python-list mailing list