Overloading the tilde operator?

Ben Finney bignose+hates-spam at benfinney.id.au
Fri Feb 2 00:21:54 EST 2007


James Stroud <jstroud at mbi.ucla.edu> writes:

> Peter Otten wrote:
> > Chris wrote:
> >>I am trying to overload the __invert__ operator (~) such that it
> >>can take a second argument,
> > 
> >>>>x ~ x
> >   File "<stdin>", line 1
> >     x ~ x
> >       ^
> > SyntaxError: invalid syntax
>
> Seems an arbitrary limitation. Consider
>    - x
> and
>    x - y

Both of which are meaningful syntax in Python, hence the Python parser
accepts them and knows what operations to call on the objects.

> Which is inconsistent with limiting ~ to a unary operation.

Which is the only Python-meaningful way to use that operator, and
translates to an appropriate call on the object.

The Python runtime parser is designed to parse Python, not some
arbitrary language that someone chooses to implement in Python.

-- 
 \     "You know what would make a good story? Something about a clown |
  `\    who makes people happy, but inside he's real sad. Also, he has |
_o__)                                severe diarrhea."  -- Jack Handey |
Ben Finney




More information about the Python-list mailing list