Overloading the tilde operator?
James Stroud
jstroud at mbi.ucla.edu
Fri Feb 2 00:14:12 EST 2007
Peter Otten wrote:
> 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
Seems an arbitrary limitation. Consider
- x
and
x - y
Which is inconsistent with limiting ~ to a unary operation.
James
More information about the Python-list
mailing list