Custom operator

M

mathias

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
 
G

Gerrit

mathias said:
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.
 
D

Diez B. Roggisch

mathias said:
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?
 
M

mathias.foehr

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

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/creaties/path/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
 
G

Gerrit

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.
 
D

Diez B. Roggisch

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.
 
N

Nuff Said

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
 
T

Terry Reedy

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
 
D

Dan Bishop

Gerrit said:
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.
 
G

Greg Ewing (using news.cis.dfn.de)

Dan said:
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... :)
 
M

Mel Wilson

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 a lot of Chinese in Unicode. Very rich symbol
set. Time to re-visit the work of John Searle.

Regards. Mel.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top