473,320 Members | 2,097 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 4447
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: | last post by:
Hi all, I've been using C++ for quite a while now and I've come to the point where I need to overload new and delete inorder to track memory and probably some profiling stuff too. I know that...
34
by: Pmb | last post by:
I've been working on creating a Complex class for my own learning purpose (learn through doing etc.). I'm once again puzzled about something. I can't figure out how to overload the assignment...
16
by: gorda | last post by:
Hello, I am playing around with operator overloading and inheritence, specifically overloading the + operator in the base class and its derived class. The structure is simple: the base class...
2
by: pmatos | last post by:
Hi all, I'm overloading operator<< for a lot of classes. The question is about style. I define in each class header the prototype of the overloading as a friend. Now, where should I define the...
5
by: luca regini | last post by:
I have this code class M { ..... T operator()( size_t x, size_t y ) const { ... Operator overloading A ....} T& operator()( size_t x, size_t y )
2
by: brzozo2 | last post by:
Hello, this program might look abit long, but it's pretty simple and easy to follow. What it does is read from a file, outputs the contents to screen, and then writes them to a different file. It...
3
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj,...
2
by: Colonel | last post by:
It seems that the problems have something to do with the overloading of istream operator ">>", but I just can't find the exact problem. // the declaration friend std::istream &...
8
by: Wayne Shu | last post by:
Hi everyone, I am reading B.S. 's TC++PL (special edition). When I read chapter 11 Operator Overloading, I have two questions. 1. In subsection 11.2.2 paragraph 1, B.S. wrote "In particular,...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.