Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Custom operator

Reply
Thread Tools

Custom operator

 
 
mathias
Guest
Posts: n/a
 
      02-05-2004
I would like to define a custom operator in Python.
(It is not about overloading an existing operator but about
defining special new operators)

Is this possible without deeply manipulating the Python code?

Are side-effects to be expected?

What about operator precedence?

Can one define an operator to be right- or left -associative?

mathias
 
Reply With Quote
 
 
 
 
Gerrit
Guest
Posts: n/a
 
      02-05-2004
mathias wrote:
> I would like to define a custom operator in Python.
> (It is not about overloading an existing operator but about
> defining special new operators)


You cannot define new syntax in Python.

This is on purpose, Python would be a different language if it could.
"import this"

Why do you want it?

Gerrit.

--
PrePEP: Builtin path type
http://people.nl.linux.org/~gerrit/c.../pep-xxxx.html
Asperger's Syndrome - a personal approach:
http://people.nl.linux.org/~gerrit/english/

 
Reply With Quote
 
 
 
 
Diez B. Roggisch
Guest
Posts: n/a
 
      02-05-2004
mathias wrote:

> I would like to define a custom operator in Python.
> (It is not about overloading an existing operator but about
> defining special new operators)
>
> Is this possible without deeply manipulating the Python code?
>
> Are side-effects to be expected?
>
> What about operator precedence?
>
> Can one define an operator to be right- or left -associative?


AFAIK thats part of the python grammar - so you can't do it. If your really
need such stuff, you might be able to create a custom language, or a
preprocessor.

What do you actually want to introduce?

--
Regards,

Diez B. Roggisch
 
Reply With Quote
 
mathias.foehr@mails.lu
Guest
Posts: n/a
 
      02-06-2004
I want to define new operators because mathematicians use special operators (
eg circle plus (+), circle times (*), arrows ->
) since centuries. It increases readability in this area and people have
got used to it.

By the way, are there hooks in python to preprocess a statement before the
python compiler
takes care of it?

Mathias

At 11:07 PM 5/2/2004 +0100, Gerrit wrote:
>mathias wrote:
> > I would like to define a custom operator in Python.
> > (It is not about overloading an existing operator but about
> > defining special new operators)

>
>You cannot define new syntax in Python.
>
>This is on purpose, Python would be a different language if it could.
>"import this"
>
>Why do you want it?
>
>Gerrit.
>
>--
>PrePEP: Builtin path type
> http://people.nl.linux.org/~gerrit/c.../pep-xxxx.html
>Asperger's Syndrome - a personal approach:
> http://people.nl.linux.org/~gerrit/english/


Mathias Foehr et Béatrice Gennart
69a, route de Kreuzerbuch
L-8370 Hobscheid
GD Luxembourg


 
Reply With Quote
 
Gerrit
Guest
Posts: n/a
 
      02-06-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> I want to define new operators because mathematicians use special operators
> (
> eg circle plus (+), circle times (*), arrows ->
> ) since centuries. It increases readability in this area and people have
> got used to it.


Hm, but all syntax would have to be ASCII anyway. A + can be overridden
with __add__, a * with __mul__, and maybe there would even be an ugly
hack to override -> using __sub__ and __gt__, although I don't think so.

But adding new characters is certainly not possible.

Gerrit.

--
PrePEP: Builtin path type
http://people.nl.linux.org/~gerrit/c.../pep-xxxx.html
Asperger's Syndrome - a personal approach:
http://people.nl.linux.org/~gerrit/english/

 
Reply With Quote
 
Diez B. Roggisch
Guest
Posts: n/a
 
      02-06-2004
(E-Mail Removed) wrote:

> I want to define new operators because mathematicians use special
> operators (
> eg circle plus (+), circle times (*), arrows ->
> ) since centuries. It increases readability in this area and people have
> got used to it.


Then I suggest you wait until we from the tu berlin finish opal 2 - a
functional programming language which features declaration not only of
infix-operators, but even mix-fixes like

_ [ _ ]

for element access and the like. You can also specify operator precedence
and associativity.

In the already existing Opal btw. you can already create arbitrary
infix-ops, but afaik you need to use parentheses to eliminate ambiguities.

> By the way, are there hooks in python to preprocess a statement before the
> python compiler
> takes care of it?


Not that I know of - but I'm not expert on this. A few weeks ago somebody
announced that he was working on lisp-style makros - maybe he found a way.
Search google.

--
Regards,

Diez B. Roggisch
 
Reply With Quote
 
Nuff Said
Guest
Posts: n/a
 
      02-06-2004
On Fri, 06 Feb 2004 07:56:28 +0100, mathias.foehr wrote:

> I want to define new operators because mathematicians use special operators (
> eg circle plus (+), circle times (*), arrows ->
> ) since centuries. It increases readability in this area and people have
> got used to it.


But there are so many of them ...

Mathematicians are e.g. also used to LaTeX terminology for
writing their papers; so it might be a better approach to
define a set of functions, classes, methods etc. using names
from LaTeX (instead of trying to add 'real' new operators to
Python).

Moreover: observe, that e.g. oplus (+) has different meanings
(depending on what area of maths you are working in). The same
holds for almost all operators.

HTH / Nuff

 
Reply With Quote
 
Terry Reedy
Guest
Posts: n/a
 
      02-06-2004

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) u...
>I want to define new operators because mathematicians use special

operators
> (eg circle plus (+), circle times (*), arrows ->) since centuries.
> It increases readability in this area and people have got used to it.


I am sympathetic to the desire and for the same reason, but there are
practical problems with operator extensibility, which is why most computer
languages lack it.

Your choices:

* Reuse existing operators (which Python does allow).

* Use overt functions and function calls. Operators are covert functions
with an alternate and somewhat ambiguous syntax for function calls, which
is why associativity and precedence rules are needed to disambiguate.
Short names are not all that bad, and meaningful short name also have
readability benefits.

* Write custom additions to some version of the interpreter.

* Write a preprocessor that does a partial parse to identify custom
operators and the blocks governed by each and then use rules to rewrite
code with overt function calls. If the preprocessor is an external
program, it can be written in any language. One can preprocess 'in line',
by quoting the code, manipulating it, and then compile or execute. This
works best when the custom manipulation is pretty localized.

* Use another language

> By the way, are there hooks in python to preprocess a statement before

the
> python compiler takes care of it?


No, there is no built-in macro facility. The best you can do is to quote
code to protect it from the initial compiler pass so you can manipulate it
as a runtime string before really compiling it.

Terry J. Reedy




 
Reply With Quote
 
Dan Bishop
Guest
Posts: n/a
 
      02-07-2004
Gerrit <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> (E-Mail Removed) wrote:
> > I want to define new operators because mathematicians use special operators
> > (
> > eg circle plus (+), circle times (*), arrows ->
> > ) since centuries. It increases readability in this area and people have
> > got used to it.

>
> Hm, but all syntax would have to be ASCII anyway.


There are still three ASCII characters that aren't used in Python:
'@', '$', and '?'. I don't see any obvious mathematical meaning for
them, though.
 
Reply With Quote
 
Greg Ewing (using news.cis.dfn.de)
Guest
Posts: n/a
 
      02-19-2004
Dan Bishop wrote:
> There are still three ASCII characters that aren't used in Python:
> '@', '$', and '?'. I don't see any obvious mathematical meaning for
> them, though.


There was an idea floating around a while back to define
'@' as matrix multiplication, so the Numeric people could
use it. Hasn't happened yet, though.

Hmmm... Now that we can have Unicode source, maybe any
otherwise-unused Unicode punctuation character could be
allowed as an operator. The corresponding method name
would be character's Unicode name with double underscores
attached.

There's the problem of precedence... maybe that could
be defined by the Unicode collating sequence...

--
Greg Ewing, Computer Science Dept,
University of Canterbury,
Christchurch, New Zealand
http://www.cosc.canterbury.ac.nz/~greg

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
T::operator int () const ambiguous with T::operator Handle () const? Tim Clacy C++ 15 05-30-2005 02:14 AM
Member operators operator>>() and operator<<() Alex Vinokur C++ 3 03-20-2005 03:11 PM
operator*(Foo) and operator*(int) const: ISO C++ says that these are ambiguous: Alex Vinokur C++ 4 11-26-2004 11:46 PM
Operator overloading on "default" operator John Smith C++ 2 10-06-2004 10:22 AM
Q: operator void* or operator bool? Jakob Bieling C++ 2 03-05-2004 04:27 PM



Advertisments