Overloading the tilde operator?

Peter Otten __peter__ at web.de
Thu Feb 1 14:08:15 EST 2007


Chris wrote:

> I am trying to overload the __invert__ operator (~) such that
> it can take a second argument, other than
> self, so that I can express:
> 
> x ~ y
> 
> by using:
> 
> def __invert__(self, other): <do something>
> 
> for example. Is this possible?

No, you will get a syntax error before python even look up the names:

>>> x
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
NameError: name 'x' is not defined
>>> x ~ x
  File "<stdin>", line 1
    x ~ x
      ^
SyntaxError: invalid syntax

Peter



More information about the Python-list mailing list