Overloading the tilde operator?

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

    #1

    Overloading the tilde operator?

    I am trying to overload the __invert__ operator (~) such that
    it can take a second argument, other than
    self, so that I can express:

    x ~ y

    by using:

    def __invert__(self , other): <do something>

    for example. Is this possible?

    Thanks in advance,


  • Peter Otten

    #2
    Re: Overloading the tilde operator?

    Chris wrote:
    I am trying to overload the __invert__ operator (~) such that
    it can take a second argument, other than
    self, so that I can express:
    >
    x ~ y
    >
    by using:
    >
    def __invert__(self , other): <do something>
    >
    for example. Is this possible?
    No, you will get a syntax error before python even look up the names:
    >>x
    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    NameError: name 'x' is not defined
    >>x ~ x
    File "<stdin>", line 1
    x ~ x
    ^
    SyntaxError: invalid syntax

    Peter

    Comment

    • bearophileHUGS@lycos.com

      #3
      Re: Overloading the tilde operator?

      Peter Otten:
      No, you will get a syntax error before python even look up the names:
      There are some tricks that allow the use of "undefined" symbols in
      Python too, but they are probably just toys. I have recently posted a
      recipe in the cookbook for that.

      Bye,
      bearophile

      Comment

      • James Stroud

        #4
        Re: Overloading the tilde operator?

        Peter Otten wrote:
        Chris wrote:
        >
        >
        >>I am trying to overload the __invert__ operator (~) such that
        >>it can take a second argument, other than
        >>self, so that I can express:
        >>
        >>x ~ y
        >>
        >>by using:
        >>
        >>def __invert__(self , other): <do something>
        >>
        >>for example. Is this possible?
        >
        >
        No, you will get a syntax error before python even look up the names:
        >
        >
        >>>>x
        >
        Traceback (most recent call last):
        File "<stdin>", line 1, in ?
        NameError: name 'x' is not defined
        >
        >>>>x ~ x
        >
        File "<stdin>", line 1
        x ~ x
        ^
        SyntaxError: invalid syntax
        >
        Peter
        Seems an arbitrary limitation. Consider

        - x

        and

        x - y

        Which is inconsistent with limiting ~ to a unary operation.

        James

        Comment

        • Ben Finney

          #5
          Re: Overloading the tilde operator?

          James Stroud <jstroud@mbi.uc la.eduwrites:
          Peter Otten wrote:
          Chris wrote:
          >I am trying to overload the __invert__ operator (~) such that it
          >can take a second argument,
          >>>x ~ x
          File "<stdin>", line 1
          x ~ x
          ^
          SyntaxError: invalid syntax
          >
          Seems an arbitrary limitation. Consider
          - x
          and
          x - y
          Both of which are meaningful syntax in Python, hence the Python parser
          accepts them and knows what operations to call on the objects.
          Which is inconsistent with limiting ~ to a unary operation.
          Which is the only Python-meaningful way to use that operator, and
          translates to an appropriate call on the object.

          The Python runtime parser is designed to parse Python, not some
          arbitrary language that someone chooses to implement in Python.

          --
          \ "You know what would make a good story? Something about a clown |
          `\ who makes people happy, but inside he's real sad. Also, he has |
          _o__) severe diarrhea." -- Jack Handey |
          Ben Finney

          Comment

          • James Stroud

            #6
            Re: Overloading the tilde operator?

            Ben Finney wrote:
            James Stroud <jstroud@mbi.uc la.eduwrites:
            >
            >
            >>Peter Otten wrote:
            >>
            >>>Chris wrote:
            >>>
            >>>>I am trying to overload the __invert__ operator (~) such that it
            >>>>can take a second argument,
            >>>
            >>>>>>x ~ x
            >>>
            >> File "<stdin>", line 1
            >> x ~ x
            >> ^
            >>>SyntaxErro r: invalid syntax
            >>
            >>Seems an arbitrary limitation. Consider
            > - x
            >>and
            > x - y
            >
            >
            Both of which are meaningful syntax in Python, hence the Python parser
            accepts them and knows what operations to call on the objects.
            >
            >
            >>Which is inconsistent with limiting ~ to a unary operation.
            >
            >
            Which is the only Python-meaningful way to use that operator, and
            translates to an appropriate call on the object.
            >
            The Python runtime parser is designed to parse Python, not some
            arbitrary language that someone chooses to implement in Python.
            >
            You haven't addressed why the limitation isn't arbitrary. You have only
            told us what we already know. So your argument, therefore, is not one.

            James

            Comment

            • George Sakkis

              #7
              Re: Overloading the tilde operator?

              On Feb 2, 12:49 am, James Stroud <jstr...@mbi.uc la.eduwrote:
              Ben Finney wrote:
              >
              The Python runtime parser is designed to parse Python, not some
              arbitrary language that someone chooses to implement in Python.
              >
              You haven't addressed why the limitation isn't arbitrary.
              Indeed, and that's because it is arbitrary. Python has the arbitrary
              limitation that it's not Perl (or C, or Lisp or what have you).

              George

              Comment

              • Ben Finney

                #8
                Re: Overloading the tilde operator?

                James Stroud <jstroud@mbi.uc la.eduwrites:
                Ben Finney wrote:
                The Python runtime parser is designed to parse Python, not some
                arbitrary language that someone chooses to implement in Python.
                >
                You haven't addressed why the limitation isn't arbitrary.
                Good thing I wasn't trying to do that, then. I was pointing out the
                source of the limitation.

                The Python syntax parser must follow the rules of the Python
                language. If you accept that premise, it follows that the '~' operator
                is unary only. If you *don't* accept that premise, I have no help to
                offer.

                --
                \ "I cannot conceive that anybody will require multiplications at |
                `\ the rate of 40,000 or even 4,000 per hour ..." -- F. H. Wales, |
                _o__) 1936 |
                Ben Finney

                Comment

                • Neil Cerutti

                  #9
                  Re: Overloading the tilde operator?

                  On 2007-02-02, Ben Finney <bignose+hate s-spam@benfinney. id.auwrote:
                  James Stroud <jstroud@mbi.uc la.eduwrites:
                  >Ben Finney wrote:
                  The Python runtime parser is designed to parse Python, not
                  some arbitrary language that someone chooses to implement in
                  Python.
                  >>
                  >You haven't addressed why the limitation isn't arbitrary.
                  >
                  Good thing I wasn't trying to do that, then. I was pointing out
                  the source of the limitation.
                  >
                  The Python syntax parser must follow the rules of the Python
                  language. If you accept that premise, it follows that the '~'
                  operator is unary only. If you *don't* accept that premise, I
                  have no help to offer.
                  There's been only one (or two?) languages in history that
                  attempted to provide programmers with the ability to implement
                  new infix operators, including defining precedence level and
                  associativity (I can't think of the name right now).

                  C++, for example, works the same way as Python here. You can
                  override most of the operators, but you cannot change their
                  arity, associativity, or precedence level.

                  --
                  Neil Cerutti
                  Let us join David and Lisa in the celebration of their wedding and bring their
                  happiness to a conclusion. --Church Bulletin Blooper

                  Comment

                  • greg

                    #10
                    Re: Overloading the tilde operator?

                    James Stroud wrote:
                    You haven't addressed why the limitation isn't arbitrary.
                    It's not arbitrary because there is a built-in meaning
                    for infix minus, but not for infix tilde.

                    Python doesn't go out of its way to provide operators
                    which aren't used by at least one built-in type.

                    --
                    Greg

                    Comment

                    • Dave Benjamin

                      #11
                      Re: Overloading the tilde operator?

                      Neil Cerutti wrote:
                      There's been only one (or two?) languages in history that
                      attempted to provide programmers with the ability to implement
                      new infix operators, including defining precedence level and
                      associativity (I can't think of the name right now).
                      You're probably thinking of SML or Haskell. OCaml also allows you to
                      define new infix operators, but the associativities are fixed (and
                      determined by what punctuation you use).

                      Comment

                      • greg

                        #12
                        Re: Overloading the tilde operator?

                        Dave Benjamin wrote:
                        Neil Cerutti wrote:
                        >
                        >There's been only one (or two?) languages in history that
                        >attempted to provide programmers with the ability to implement
                        >new infix operators, including defining precedence level and
                        >associativit y (I can't think of the name right now).
                        >
                        You're probably thinking of SML or Haskell. OCaml also allows you to
                        define new infix operators, but the associativities are fixed (and
                        determined by what punctuation you use).
                        Prolog lets you do this, too.

                        In Smalltalk, you can use just about any sequence of
                        non-letters as an infix operator, but it has no notion
                        of precedence, even for the built-in operators.

                        I think SNOBOL may have had something for defining new
                        operators, but I can't remember the details.

                        --
                        Greg

                        Comment

                        • gatti@dsdata.it

                          #13
                          Re: Overloading the tilde operator?

                          On Feb 8, 7:02 am, Dave Benjamin <r...@lackingta lent.comwrote:
                          Neil Cerutti wrote:
                          There's been only one (or two?) languages in history that
                          attempted to provide programmers with the ability to implement
                          new infix operators, including defining precedence level and
                          associativity (I can't think of the name right now).
                          >
                          You're probably thinking of SML or Haskell. OCaml also allows you to
                          define new infix operators, but the associativities are fixed (and
                          determined by what punctuation you use).
                          Also some flavours of Prolog, as descrived in the classic book by
                          Clocksin & Mellish.

                          Regarding the OP, I hope his need for an infix tilde operator is
                          overestimated; there are plenty of infix operators that can be abused,
                          and at least one of them should be unused and available for
                          redefinition.

                          Lorenzo Gatti

                          Comment

                          • Markus Triska

                            #14
                            Re: Overloading the tilde operator?

                            gatti@dsdata.it writes:
                            Also some flavours of Prolog, as descrived in the classic book by
                            op/3 is part of the Prolog ISO standard:



                            so every compliant implementation has it.

                            Comment

                            Working...