Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Custom operator (http://www.velocityreviews.com/forums/t327972-custom-operator.html)

mathias 02-05-2004 09:36 PM

Custom operator
 
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

Gerrit 02-05-2004 10:07 PM

Re: Custom operator
 
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/


Diez B. Roggisch 02-05-2004 10:12 PM

Re: Custom operator
 
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

mathias.foehr@mails.lu 02-06-2004 06:56 AM

Re: Custom operator
 
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



Gerrit 02-06-2004 10:49 AM

Re: Custom operator
 
mathias.foehr@mails.lu 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/


Diez B. Roggisch 02-06-2004 10:51 AM

Re: Custom operator
 
mathias.foehr@mails.lu 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

Nuff Said 02-06-2004 02:48 PM

Re: Custom operator
 
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


Terry Reedy 02-06-2004 06:42 PM

Re: Custom operator
 

<mathias.foehr@mails.lu> wrote in message
news:6.0.1.1.0.20040206074906.02624b30@www.mails.l 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





Dan Bishop 02-07-2004 01:57 AM

Re: Custom operator
 
Gerrit <gerrit@nl.linux.org> wrote in message news:<mailman.1296.1076064694.12720.python-list@python.org>...
> mathias.foehr@mails.lu 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.

Greg Ewing (using news.cis.dfn.de) 02-19-2004 02:27 AM

Unicode operators? (Re: Custom operator)
 
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



All times are GMT. The time now is 06:52 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.