-2
class MyClass:
    def say():
        print("hello")

mc = MyClass()
mc.say()

I am getting error: TypeError: say() takes no arguments (1 given). What I am doing wrong?

Alan Kavanagh
  • 9,425
  • 7
  • 41
  • 65
Dmitry Bubnenkov
  • 9,415
  • 19
  • 85
  • 145
  • I can keep adding duplicates, but you should've found these links in a heartbeat with google. – cs95 Sep 27 '17 at 13:27

1 Answers1

9

This is because methods in a class expect the first argument to be self. This self parameter is passed internally by python as it always sends a reference to itself when calling a method, even if it's unused within the method

class MyClass:
    def say(self):
        print("hello")

mc = MyClass()
mc.say()
>> hello

Alternatively, you can make the method static and remove the self parameter

class MyClass:
    @staticmethod
    def say():
        print("hello")

mc = MyClass()
mc.say()
>> hello
Alan Kavanagh
  • 9,425
  • 7
  • 41
  • 65