No method overloading

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

    #1

    No method overloading

    Hey,
    Please correct me if I'm wrong but Python doesn't support method
    overload, right?
    --
    def method(self):
    #code
    def method(self, data):
    #code
    --
    The last declaration of method() erase the previous one (like
    JavaScript).
    Thanks.
  • MeTheGameMakingGuy

    #2
    Re: No method overloading

    On Aug 24, 6:15 pm, Hussein B <hubaghd...@gma il.comwrote:
    Hey,
    Please correct me if I'm wrong but Python doesn't support method
    overload, right?
    --
    def method(self):
     #code
    def method(self, data):
     #code
    --
    The last declaration of method() erase the previous one (like
    JavaScript).
    Thanks.
    Correct. The second declaration overwrites the first. Give data a
    default argument instead:
    def method(self, data=None):
    if data is None:
    # Normal code
    else:
    # data included code

    Not sure how Pythonic that is, but it should work the way you're
    requesting... I think.

    Comment

    • Fredrik Lundh

      #3
      Re: No method overloading

      Hussein B wrote:
      Please correct me if I'm wrong but Python doesn't support method
      overload, right?
      --
      def method(self):
      #code
      def method(self, data):
      #code
      --
      The last declaration of method() erase the previous one (like
      JavaScript).
      in Python, methods are callable attributes, and an attribute can only
      have one value. you can use default arguments to work around this:

      def method(self, data=None):
      if data is None:
      #code
      else:
      #code

      </F>

      Comment

      • alex23

        #4
        Re: No method overloading

        On Aug 24, 6:15 pm, Hussein B <hubaghd...@gma il.comwrote:
        Please correct me if I'm wrong but Python doesn't support method
        overload, right?
        Guido once wrote an article on rolling your own:



        Comment

        Working...