0

This code is correctly running.

class Apple:
    def bag(self,x,y):
        print("sssss")
    def bag(self,y):
        print("dddddd")
a1=Apple()
a1.bag(2)

But this code is not running

class Apple:
    def bag(self,x,y):
        print("sssss")

def bag(self,y):
    print("dddddd")

a1=Apple()
a1.bag(2,5)

What is the problem?

Amir Charkhi
  • 603
  • 6
  • 19
Madusanka
  • 1
  • 1
  • Python does not support method overloading in the way that C++ does for example. See link above. – Cory Kramer Aug 10 '21 at 12:45
  • `Apple.bag` is a method. `bag` is just a function unrelated to `Apple`. Your first example doesn't overload `Apple.bag`; it just redefines it. Notice that `a1.bag(2, 5)` would raise a `TypeError` in that example. – chepner Aug 10 '21 at 13:39

0 Answers0