Re: <build-in function> incompatible with <function>
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.python archive

Re: <build-in function> incompatible with <function>

From: James Stroud <jstroud@ucla.edu>
Date: Tue Jan 31 2006 - 03:51:12 CET

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
Received on Tue Feb 7 20:18:34 2006