overload builtin operator

Shaun shmem at netspace.net.au
Thu Aug 25 04:37:36 EDT 2005


Hi,

I'm trying to overload the divide operator in python for basic arithmetic.
eg. 10/2 ... no classes involved.

I am attempting to redefine operator.__div__ as follows:

     # my divide function
     def safediv(a,b):
         return ...

     # reassign buildin __div__
     import operator
     operator.__div__ = safediv

The operator.__dict__ seems to be updated OK but the '/' operator still  
calls buildin __div__

Does anyone know if this is possible and if I'm going along the correct  
path with my attempts above?
Is it possible to do this using a C extention?

Regards,
Shaun.



More information about the Python-list mailing list