Functions, Operators, and Overloading?

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

    #1

    Functions, Operators, and Overloading?

    Hello:

    Maybe I am missing something, but from what I've seen,
    it is not possible to overload functions in Python. That
    is I can't have a
    def func1 (int1, string1):
    and a
    def func1 (int1, int3, string1, string2):
    without the second func1 overwriting the first.

    However, operators can be overloaded.
    So can I define a new operator? If so, can I
    define func1 as an operator?

    (on the side, I have always wanted to define the
    ++ operator as +=1. Is that possible?)

    Thanks in advance:
    Michael Yanowitz

  • Brian Beck

    #2
    Re: Functions, Operators, and Overloading?

    Michael Yanowitz wrote:
    Maybe I am missing something, but from what I've seen,
    it is not possible to overload functions in Python. That
    is I can't have a
    def func1 (int1, string1):
    and a
    def func1 (int1, int3, string1, string2):
    without the second func1 overwriting the first.
    Correct.
    However, operators can be overloaded.
    So can I define a new operator? If so, can I
    define func1 as an operator?
    No. Operators in Python are merely syntax for "magic methods" on the
    corresponding type. For instance, x + y would call x.__add__(y). In this
    sense you are not really "overloadin g" the operator, you are simply
    defining or overwriting its behavior (just like above, where the second
    func1 overwrites the previous).
    (on the side, I have always wanted to define the
    ++ operator as +=1. Is that possible?)
    No, for the reason above -- there is no magic method associated with ++,
    which isn't a real Python operator.

    --
    Brian Beck
    Adventurer of the First Order

    Comment

    • Gerhard Fiedler

      #3
      Re: Functions, Operators, and Overloading?

      On 2006-07-24 14:30:31, Brian Beck wrote:
      Michael Yanowitz wrote:
      > Maybe I am missing something, but from what I've seen,
      >it is not possible to overload functions in Python. That
      >is I can't have a
      > def func1 (int1, string1):
      > and a
      > def func1 (int1, int3, string1, string2):
      > without the second func1 overwriting the first.
      >
      Correct.
      Can you write a function that accepts any number of arguments? And then
      branch based on the number of arguments supplied?

      I guess you can do that with a list as only argument. But can that be done
      using the "normal" function argument notation?

      Gerhard

      Comment

      • Stefan Behnel

        #4
        Re: Functions, Operators, and Overloading?

        Gerhard Fiedler schrieb:
        On 2006-07-24 14:30:31, Brian Beck wrote:
        >
        >Michael Yanowitz wrote:
        >> Maybe I am missing something, but from what I've seen,
        >>it is not possible to overload functions in Python. That
        >>is I can't have a
        >> def func1 (int1, string1):
        >> and a
        >> def func1 (int1, int3, string1, string2):
        >> without the second func1 overwriting the first.
        >Correct.
        >
        Can you write a function that accepts any number of arguments? And then
        branch based on the number of arguments supplied?
        >
        I guess you can do that with a list as only argument. But can that be done
        using the "normal" function argument notation?
        I guess you mean something like

        func1(int1, arg2, *args):
        if len(args) == 2:
        ...
        elif not args:
        ...
        else:
        raise ...


        Stefan

        Comment

        • bearophileHUGS@lycos.com

          #5
          Re: Functions, Operators, and Overloading?

          Michael Yanowitz:
          Maybe I am missing something, but from what I've seen,
          it is not possible to overload functions in Python.
          Maybe here you can find some ideas:




          (on the side, I have always wanted to define the
          ++ operator as +=1. Is that possible?)
          That's not possible. Maybe you can create an inc() function, similar to
          the Pascal one, that with a bit of stack-based magic may increment the
          value its called on, but I think this is a bad idea. Leading/trailing
          ++/-- are quite bad for a language that tries to be clear and to avoid
          programmer errors.

          Bye,
          bearophile

          Comment

          • Gerhard Fiedler

            #6
            Re: Functions, Operators, and Overloading?

            On 2006-07-24 15:05:53, Stefan Behnel wrote:
            >>> Maybe I am missing something, but from what I've seen,
            >>>it is not possible to overload functions in Python. That
            >>>is I can't have a
            >>> def func1 (int1, string1):
            >>> and a
            >>> def func1 (int1, int3, string1, string2):
            >>> without the second func1 overwriting the first.
            >>Correct.
            >>
            >Can you write a function that accepts any number of arguments? And then
            >branch based on the number of arguments supplied?
            >>
            >I guess you can do that with a list as only argument. But can that be done
            >using the "normal" function argument notation?
            >
            I guess you mean something like
            >
            func1(int1, arg2, *args):
            if len(args) == 2:
            ...
            elif not args:
            ...
            else:
            raise ...
            Exactly. So in a way, the OP's question whether such an overload is
            possible has two answers: a formal overload (two separate function
            definitions) is not possible, but a functional overload (two definitions
            for the different parameter numbers inside one function) is possible.

            Thanks,
            Gerhard

            Comment

            • Bruno Desthuilliers

              #7
              Re: Functions, Operators, and Overloading?

              Michael Yanowitz a écrit :
              Hello:
              >
              Maybe I am missing something, but from what I've seen,
              it is not possible to overload functions in Python. That
              is I can't have a
              def func1 (int1, string1):
              and a
              def func1 (int1, int3, string1, string2):
              without the second func1 overwriting the first.
              You may want to have a look at Philip Eby's dispatch package. Here's an
              introduction:
              http://peak.telecommunity.com/DevCen...sitorRevisited
              However, operators can be overloaded.
              So can I define a new operator?
              No.

              Comment

              Working...