Custom operator

Discussion in 'Python' started by mathias, Feb 5, 2004.

  1. mathias

    mathias Guest

    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
     
    mathias, Feb 5, 2004
    #1
    1. Advertising

  2. mathias

    Gerrit Guest

    Gerrit, Feb 5, 2004
    #2
    1. Advertising

  3. 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
     
    Diez B. Roggisch, Feb 5, 2004
    #3
  4. mathias

    Guest

    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/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
     
    , Feb 6, 2004
    #4
  5. mathias

    Gerrit Guest

    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/creaties/path/pep-xxxx.html
    Asperger's Syndrome - a personal approach:
    http://people.nl.linux.org/~gerrit/english/
     
    Gerrit, Feb 6, 2004
    #5
  6. 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
     
    Diez B. Roggisch, Feb 6, 2004
    #6
  7. mathias

    Nuff Said Guest

    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
     
    Nuff Said, Feb 6, 2004
    #7
  8. mathias

    Terry Reedy Guest

    <> wrote in message
    news:...
    >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
     
    Terry Reedy, Feb 6, 2004
    #8
  9. mathias

    Dan Bishop Guest

    Gerrit <> wrote in message news:<>...
    > 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.
     
    Dan Bishop, Feb 7, 2004
    #9
  10. 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
     
    Greg Ewing (using news.cis.dfn.de), Feb 19, 2004
    #10
  11. mathias

    Mel Wilson Guest

    Re: Unicode operators?

    In article <c116tu$1ak718$-berlin.de>,
    "Greg Ewing (using news.cis.dfn.de)" <> wrote:
    >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.

    >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.
     
    Mel Wilson, Mar 2, 2004
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jakob Bieling

    Q: operator void* or operator bool?

    Jakob Bieling, Mar 5, 2004, in forum: C++
    Replies:
    2
    Views:
    624
    Rob Williscroft
    Mar 5, 2004
  2. John Smith
    Replies:
    2
    Views:
    449
    Ivan Vecerina
    Oct 6, 2004
  3. Alex Vinokur
    Replies:
    4
    Views:
    3,081
    Peter Koch Larsen
    Nov 26, 2004
  4. Alex Vinokur
    Replies:
    3
    Views:
    5,068
    Jeff Schwab
    Mar 20, 2005
  5. Tim Clacy
    Replies:
    15
    Views:
    2,754
    Kanenas
    May 30, 2005
Loading...

Share This Page