-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?

AK47
  • 8,646
  • 6
  • 37
  • 61
Dmitry Bubnenkov
  • 8,444
  • 15
  • 65
  • 118
  • 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
AK47
  • 8,646
  • 6
  • 37
  • 61