Operator Overloading

Robert Kern rkern at ucsd.edu
Mon Aug 1 05:39:42 EDT 2005


Gurpreet Sachdeva wrote:
>  
> Hi,
> 
> Is there any provision in python which allows me to make my own operators?
> 
> My problem is that I need to combine two dictonaries with their keys and 
> I don't want to use any of the existing operators like '+','-','*'.
> So is there a way I can make '**' or '~' as my operators to add two 
> dictonaries? If not which all operators can I overoad?

Both ** and ~ are existing operators (although ~ is unary, not binary). 
You cannot make new ones, but you can overload all of the available ones 
for new classes (not old ones like dicts).

   http://docs.python.org/ref/numeric-types.html

With a beautiful hack, you can make your own general, pseudo-operators.

   http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/384122

-- 
Robert Kern
rkern at ucsd.edu

"In the fields of hell where the grass grows high
  Are the graves of dreams allowed to die."
   -- Richard Harter




More information about the Python-list mailing list