Custom operator

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mathias

    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
  • Diez B. Roggisch

    #2
    Re: Custom operator

    mathias wrote:
    [color=blue]
    > 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?[/color]

    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

    Comment

    • Gerrit

      #3
      Re: Custom operator

      mathias wrote:[color=blue]
      > I would like to define a custom operator in Python.
      > (It is not about overloading an existing operator but about
      > defining special new operators)[/color]

      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

      Asperger's Syndrome - a personal approach:


      Comment

      • mathias.foehr@mails.lu

        #4
        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:[color=blue]
        >mathias wrote:[color=green]
        > > I would like to define a custom operator in Python.
        > > (It is not about overloading an existing operator but about
        > > defining special new operators)[/color]
        >
        >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/[/color]

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


        Comment

        • Diez B. Roggisch

          #5
          Re: Custom operator

          mathias.foehr@m ails.lu wrote:
          [color=blue]
          > 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.[/color]

          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.
          [color=blue]
          > By the way, are there hooks in python to preprocess a statement before the
          > python compiler
          > takes care of it?[/color]

          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

          Comment

          • Gerrit

            #6
            Re: Custom operator

            mathias.foehr@m ails.lu wrote:[color=blue]
            > 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.[/color]

            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

            Asperger's Syndrome - a personal approach:


            Comment

            • Nuff Said

              #7
              Re: Custom operator

              On Fri, 06 Feb 2004 07:56:28 +0100, mathias.foehr wrote:
              [color=blue]
              > 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.[/color]

              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

              Comment

              • Terry Reedy

                #8
                Re: Custom operator


                <mathias.foehr@ mails.lu> wrote in message
                news:6.0.1.1.0. 20040206074906. 02624b30@www.ma ils.lu...[color=blue]
                >I want to define new operators because mathematicians use special[/color]
                operators[color=blue]
                > (eg circle plus (+), circle times (*), arrows ->) since centuries.
                > It increases readability in this area and people have got used to it.[/color]

                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
                [color=blue]
                > By the way, are there hooks in python to preprocess a statement before[/color]
                the[color=blue]
                > python compiler takes care of it?[/color]

                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




                Comment

                • Dan Bishop

                  #9
                  Re: Custom operator

                  Gerrit <gerrit@nl.linu x.org> wrote in message news:<mailman.1 296.1076064694. 12720.python-list@python.org >...[color=blue]
                  > mathias.foehr@m ails.lu wrote:[color=green]
                  > > 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.[/color]
                  >
                  > Hm, but all syntax would have to be ASCII anyway.[/color]

                  There are still three ASCII characters that aren't used in Python:
                  '@', '$', and '?'. I don't see any obvious mathematical meaning for
                  them, though.

                  Comment

                  • Mel Wilson

                    #10
                    Re: Unicode operators?

                    In article <c116tu$1ak718$ 1@ID-169208.news.uni-berlin.de>,
                    "Greg Ewing (using news.cis.dfn.de )" <g2h5dqi002@sne akemail.com> wrote:[color=blue]
                    >Dan Bishop wrote:[color=green]
                    >> There are still three ASCII characters that aren't used in Python:
                    >> '@', '$', and '?'. I don't see any obvious mathematical meaning for
                    >> them, though.[/color]
                    >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.[/color]

                    There's a lot of Chinese in Unicode. Very rich symbol
                    set. Time to re-visit the work of John Searle.

                    Regards. Mel.

                    Comment

                    Working...