469,964 Members | 1,613 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,964 developers. It's quick & easy.

Overloading the tilde operator?

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?

Thanks in advance,
Feb 1 '07 #1
13 4113
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
Feb 1 '07 #2
Peter Otten:
No, you will get a syntax error before python even look up the names:
There are some tricks that allow the use of "undefined" symbols in
Python too, but they are probably just toys. I have recently posted a
recipe in the cookbook for that.

Bye,
bearophile

Feb 2 '07 #3
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
Feb 2 '07 #4
James Stroud <js*****@mbi.ucla.eduwrites:
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

Feb 2 '07 #5
Ben Finney wrote:
James Stroud <js*****@mbi.ucla.eduwrites:

>>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 haven't addressed why the limitation isn't arbitrary. You have only
told us what we already know. So your argument, therefore, is not one.

James
Feb 2 '07 #6
On Feb 2, 12:49 am, James Stroud <jstr...@mbi.ucla.eduwrote:
Ben Finney wrote:
The Python runtime parser is designed to parse Python, not some
arbitrary language that someone chooses to implement in Python.

You haven't addressed why the limitation isn't arbitrary.
Indeed, and that's because it is arbitrary. Python has the arbitrary
limitation that it's not Perl (or C, or Lisp or what have you).

George

Feb 2 '07 #7
James Stroud <js*****@mbi.ucla.eduwrites:
Ben Finney wrote:
The Python runtime parser is designed to parse Python, not some
arbitrary language that someone chooses to implement in Python.

You haven't addressed why the limitation isn't arbitrary.
Good thing I wasn't trying to do that, then. I was pointing out the
source of the limitation.

The Python syntax parser must follow the rules of the Python
language. If you accept that premise, it follows that the '~' operator
is unary only. If you *don't* accept that premise, I have no help to
offer.

--
\ "I cannot conceive that anybody will require multiplications at |
`\ the rate of 40,000 or even 4,000 per hour ..." -- F. H. Wales, |
_o__) 1936 |
Ben Finney

Feb 2 '07 #8
On 2007-02-02, Ben Finney <bi****************@benfinney.id.auwrote:
James Stroud <js*****@mbi.ucla.eduwrites:
>Ben Finney wrote:
The Python runtime parser is designed to parse Python, not
some arbitrary language that someone chooses to implement in
Python.

You haven't addressed why the limitation isn't arbitrary.

Good thing I wasn't trying to do that, then. I was pointing out
the source of the limitation.

The Python syntax parser must follow the rules of the Python
language. If you accept that premise, it follows that the '~'
operator is unary only. If you *don't* accept that premise, I
have no help to offer.
There's been only one (or two?) languages in history that
attempted to provide programmers with the ability to implement
new infix operators, including defining precedence level and
associativity (I can't think of the name right now).

C++, for example, works the same way as Python here. You can
override most of the operators, but you cannot change their
arity, associativity, or precedence level.

--
Neil Cerutti
Let us join David and Lisa in the celebration of their wedding and bring their
happiness to a conclusion. --Church Bulletin Blooper
Feb 2 '07 #9
James Stroud wrote:
You haven't addressed why the limitation isn't arbitrary.
It's not arbitrary because there is a built-in meaning
for infix minus, but not for infix tilde.

Python doesn't go out of its way to provide operators
which aren't used by at least one built-in type.

--
Greg
Feb 2 '07 #10
Neil Cerutti wrote:
There's been only one (or two?) languages in history that
attempted to provide programmers with the ability to implement
new infix operators, including defining precedence level and
associativity (I can't think of the name right now).
You're probably thinking of SML or Haskell. OCaml also allows you to
define new infix operators, but the associativities are fixed (and
determined by what punctuation you use).
Feb 8 '07 #11
Dave Benjamin wrote:
Neil Cerutti wrote:
>There's been only one (or two?) languages in history that
attempted to provide programmers with the ability to implement
new infix operators, including defining precedence level and
associativity (I can't think of the name right now).

You're probably thinking of SML or Haskell. OCaml also allows you to
define new infix operators, but the associativities are fixed (and
determined by what punctuation you use).
Prolog lets you do this, too.

In Smalltalk, you can use just about any sequence of
non-letters as an infix operator, but it has no notion
of precedence, even for the built-in operators.

I think SNOBOL may have had something for defining new
operators, but I can't remember the details.

--
Greg
Feb 8 '07 #12
On Feb 8, 7:02 am, Dave Benjamin <r...@lackingtalent.comwrote:
Neil Cerutti wrote:
There's been only one (or two?) languages in history that
attempted to provide programmers with the ability to implement
new infix operators, including defining precedence level and
associativity (I can't think of the name right now).

You're probably thinking of SML or Haskell. OCaml also allows you to
define new infix operators, but the associativities are fixed (and
determined by what punctuation you use).
Also some flavours of Prolog, as descrived in the classic book by
Clocksin & Mellish.

Regarding the OP, I hope his need for an infix tilde operator is
overestimated; there are plenty of infix operators that can be abused,
and at least one of them should be unused and available for
redefinition.

Lorenzo Gatti

Feb 8 '07 #13
ga***@dsdata.it writes:
Also some flavours of Prolog, as descrived in the classic book by
op/3 is part of the Prolog ISO standard:

http://pauillac.inria.fr/~deransar/p...html#operators

so every compliant implementation has it.
Feb 9 '07 #14

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by | last post: by
2 posts views Thread by pmatos | last post: by
5 posts views Thread by luca regini | last post: by
3 posts views Thread by y-man | last post: by
2 posts views Thread by Colonel | last post: by
1 post views Thread by rainxy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.